yiisoft/html
Composer 安装命令:
composer require yiisoft/html
包简介
Handy library to generate HTML
关键字:
README 文档
README
Yii HTML
The package provides various tools to help with dynamic server-side generation of HTML:
- Tag classes
A,Address,Article,Aside,Audio,B,Body,Br,Button,Caption,Col,Colgroup,Datalist,Div,Em,Fieldset,Footer,Form,H1,H2,H3,H4,H5,H6,Header,Hr,Hgroup,Html,I,Img,Input(and specializedCheckbox,Radio,Range,File,Color),Label,Legend,Li,Link,Meta,Nav,Noscript,Ol,Optgroup,Option,P,Picture,Script,Section,Select,Small,Source,Span,Strong,Style,Table,Tbody,Td,Textarea,Tfoot,Th,Thead,Title,Tr,Track,Ul,Video. CustomTagclass that helps to generate custom tag with any attributes.- HTML widgets
ButtonGroup,CheckboxListandRadioList. - All tags content is automatically HTML-encoded. There is
NoEncodeclass designed to wrap content that should not be encoded. Htmlhelper that has static methods to generate HTML, create tags and HTML widget objects.
Note that for simple static-HTML cases, it is preferred to use HTML directly.
Requirements
- PHP 8.1 - 8.5.
Installation
The package could be installed with Composer:
composer require yiisoft/html
General usage
<?php use Yiisoft\Html\Html; use Yiisoft\Html\Tag\Meta; ?> <?= Meta::pragmaDirective('X-UA-Compatible', 'IE=edge') ?> <?= Meta::data('viewport', 'width=device-width, initial-scale=1') ?> <?= Html::cssFile( 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css', [ 'integrity' => 'sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T', 'crossorigin' => 'anonymous' ] ) ?> <?= Html::cssFile('/css/site.css', ['rel' => 'stylesheet']) ?> <?= Html::openTag('footer', ['class' => 'footer']) ?> <?= Html::openTag('div', ['class' => 'container flex-fill']) ?> <?= Html::p('', ['class' => 'float-left']) ?> <?= Html::p() ->class('float-right') ->content( 'Powered by ', Html::a( 'Yii Framework', 'https://www.yiiframework.com/', ['rel' => 'external'] ) ) ?> <?= Html::closeTag('div') ?> <?= Html::closeTag('footer') ?>
Tag objects usage
Tag classes allow working with a tag as an object and then get an HTML code by using render() method or type casting
to string. For example, the following code:
echo new \Yiisoft\Html\Tag\Div() ->content( new \Yiisoft\Html\Tag\A() ->mailto('info@example.com') ->content('contact us') ->render() ) ->encode(false) ->id('ContactEmail') ->class('red');
... will generate the following HTML:
<div id="ContactEmail" class="red"><a href="mailto:info@example.com">contact us</a></div>
Generating custom tags
To generate custom tags, use the CustomTag class. For example, the following code:
echo new \Yiisoft\Html\Tag\CustomTag('b') ->content('text') ->attribute('title', 'Important');
... will generate the following HTML:
<b title="Important">text</b>
Encoding tags content
By default, stringable objects that implement \Yiisoft\Html\NoEncodeStringableInterface are not encoded,
everything else is encoded.
To change this behavior use encode() method passing one of the following values:
null: default behavior;true: any content is encoded;false: nothing is encoded.
Note: all bundled tags and widgets implement
\Yiisoft\Html\NoEncodeStringableInterfaceinterface and are not encoded by default when passed as content. Their own content is encoded.
Examples:
// <b><i>hello</i></b> echo Html::b('<i>hello</i>'); // <b><i>hello</i></b> echo Html::b('<i>hello</i>')->encode(false); // <b><i>hello</i></b> echo Html::b(Html::i('hello')); // <b><i>hello</i></b> echo Html::b(Html::i('hello'))->encode(true);
In order to mark a string as "do not encode" you can use \Yiisoft\Html\NoEncode class:
// <b><i>hello</i></b> echo Html::b(NoEncode::string('<i>hello</i>'));
HTML widgets usage
There are multiple widgets that do not directly represent any HTML tag, but a set of tags. These help to express complex HTML in simple PHP.
ButtonGroup
Represents a group of buttons.
echo new \Yiisoft\Html\Widget\ButtonGroup() ->buttons( \Yiisoft\Html\Html::resetButton('Reset Data'), \Yiisoft\Html\Html::resetButton('Send'), ) ->containerAttributes(['class' => 'actions']) ->buttonAttributes(['form' => 'CreatePost']);
Result will be:
<div class="actions"> <button type="reset" form="CreatePost">Reset Data</button> <button type="reset" class="primary" form="CreatePost">Send</button> </div>
CheckboxList
Represents a list of checkboxes.
echo new \Yiisoft\Html\Widget\CheckboxList\CheckboxList('count') ->items([1 => 'One', 2 => 'Two', 5 => 'Five']) ->uncheckValue(0) ->value(2, 5) ->containerAttributes(['id' => 'main']);
Result will be:
<input type="hidden" name="count" value="0"> <div id="main"> <label><input type="checkbox" name="count[]" value="1"> One</label> <label><input type="checkbox" name="count[]" value="2" checked> Two</label> <label><input type="checkbox" name="count[]" value="5" checked> Five</label> </div>
RadioList
Represents a list of radio buttons.
echo new \Yiisoft\Html\Widget\RadioList\RadioList('count') ->items([1 => 'One', 2 => 'Two', 5 => 'Five']) ->uncheckValue(0) ->value(2) ->containerAttributes(['id' => 'main']);
Result will be:
<input type="hidden" name="test" value="0"> <div id="main"> <label><input type="radio" name="test" value="1"> One</label> <label><input type="radio" name="test" value="2" checked> Two</label> <label><input type="radio" name="test" value="5"> Five</label> </div>
Html helper usage
Html helper methods are static so usage is:
echo \Yiisoft\Html\Html::a('Yii Framework', 'https://www.yiiframework.com/');
Overall the helper has the following method groups.
Creating tag objects
Custom tags
- tag
- normalTag
- voidTag
Base tags
- b
- div
- em
- i
- hr
- meta
- p
- br
- script
- noscript
- span
- strong
- small
- style
- title
Media tags
- img
- picture
- audio
- video
- track
- source
Heading tags
- h1
- h2
- h3
- h4
- h5
- h6
Section tags
- html
- body
- article
- section
- nav
- aside
- hgroup
- header
- footer
- address
List tags
- ul
- ol
- li
Hyperlink tags
- a
- mailto
Link tags
- link
- cssFile
- javaScriptFile
Form tags
- button
- buttonInput
- checkbox
- color
- datalist
- fieldset
- file
- fileInput
- form
- hiddenInput
- input
- label
- legend
- optgroup
- option
- passwordInput
- radio
- range
- resetButton
- resetInput
- select
- submitButton
- submitInput
- textarea
- textInput
Table tags
- table
- caption
- colgroup
- col
- thead
- tbody
- tfoot
- tr
- th
- td
Generating tag parts
- openTag
- closeTag
- renderTagAttributes
Creating HTML widget objects
- radioList
- checkboxList
Working with tag attributes
- generateId
- getArrayableName
- getNonArrayableName
- normalizeRegexpPattern
Encode and escape special characters
- encode
- encodeAttribute
- encodeUnquotedAttribute
- escapeJavaScriptStringValue
Working with CSS styles and classes
- addCssStyle
- removeCssStyle
- addCssClass
- removeCssClass
- cssStyleFromArray
- cssStyleToArray
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii HTML is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/html 适用场景与选型建议
yiisoft/html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 593.55k 次下载、GitHub Stars 达 58, 最近一次更新时间为 2019 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/html 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
HTML and form generation
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
Reusable utilities library for Lacus Solutions' packages (type description, HTML escaping, random sequences)
Yii2 block extension
A rails-inspired form builder for PHP
统计信息
- 总下载量: 593.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 58
- 点击次数: 14
- 依赖项目数: 58
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-08-12