yiisoft / html
用于生成HTML的便捷库
3.7.0
2024-09-18 11:48 UTC
Requires
- php: ^8.0
- yiisoft/arrays: ^2.0|^3.0
- yiisoft/json: ^1.0
Requires (Dev)
- infection/infection: ^0.26.19
- maglnet/composer-require-checker: ^4.4
- phpunit/phpunit: ^9.5
- rector/rector: ^1.0.0
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^5.25
README
Yii HTML
该包提供各种工具,以帮助动态生成服务器端HTML。
- 标签类
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(以及专门的Checkbox、Radio、Range、File)、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。 - 帮助生成带有任意属性的定制标签的
CustomTag类。 - HTML小部件
ButtonGroup、CheckboxList和RadioList。 - 所有标签的内容将自动进行HTML编码。存在
NoEncode类,用于包装不需要编码的内容。 - 具有生成HTML、创建标签和HTML小部件对象的静态方法的
Html辅助程序。
注意,对于简单的静态HTML情况,建议直接使用HTML。
要求
- PHP 8.0或更高版本。
安装
可以使用Composer安装该包。
composer require yiisoft/html
一般用法
<?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.bootstrap.ac.cn/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://yiiframework.cn/', ['rel' => 'external'] ) ) ?> <?= Html::closeTag('div') ?> <?= Html::closeTag('footer') ?>
标签对象的使用
标签类允许将标签作为对象进行处理,然后通过使用render()方法或类型转换到字符串来获取HTML代码。例如,以下代码
echo \Yiisoft\Html\Tag\Div::tag() ->content( \Yiisoft\Html\Tag\A::tag() ->mailto('info@example.com') ->content('contact us') ->render() ) ->encode(false) ->id('ContactEmail') ->class('red');
...将生成以下HTML
<div id="ContactEmail" class="red"><a href="mailto:info@example.com">contact us</a></div>
生成自定义标签
要生成自定义标签,请使用CustomTag类。例如,以下代码
echo \Yiisoft\Html\Tag\CustomTag::name('b') ->content('text') ->attribute('title', 'Important');
...将生成以下HTML
<b title="Important">text</b>
标签内容编码
默认情况下,实现\Yiisoft\Html\NoEncodeStringableInterface的字符串化对象不会被编码,其他所有内容都会被编码。
要更改此行为,请使用encode()方法传递以下值之一
null:默认行为;true:任何内容都会被编码;false:不进行任何编码。
注意:所有捆绑的标签和控件都实现了
\Yiisoft\Html\NoEncodeStringableInterface接口,并且默认情况下在作为内容传递时不进行编码。它们自己的内容将被编码。
示例
// <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);
为了将字符串标记为“不编码”,可以使用\Yiisoft\Html\NoEncode类
// <b><i>hello</i></b> echo Html::b(NoEncode::string('<i>hello</i>'));
HTML小部件的使用
存在多个小部件,它们不直接表示任何HTML标签,而是一组标签。这些帮助在简单的PHP中表达复杂的HTML。
ButtonGroup
表示一组按钮。
echo \Yiisoft\Html\Widget\ButtonGroup::create() ->buttons( \Yiisoft\Html\Html::resetButton('Reset Data'), \Yiisoft\Html\Html::resetButton('Send'), ) ->containerAttributes(['class' => 'actions']) ->buttonAttributes(['form' => 'CreatePost']);
结果将是
<div class="actions"> <button type="reset" form="CreatePost">Reset Data</button> <button type="reset" class="primary" form="CreatePost">Send</button> </div>
CheckboxList
表示一组复选框。
echo \Yiisoft\Html\Widget\CheckboxList\CheckboxList::create('count') ->items([1 => 'One', 2 => 'Two', 5 => 'Five']) ->uncheckValue(0) ->value(2, 5) ->containerAttributes(['id' => 'main']);
结果将是
<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
表示一组单选按钮。
echo \Yiisoft\Html\Widget\RadioList\RadioList::create('count') ->items([1 => 'One', 2 => 'Two', 5 => 'Five']) ->uncheckValue(0) ->value(2) ->containerAttributes(['id' => 'main']);
结果将是
<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辅助程序的使用
Html辅助程序方法是静态的,因此使用方式是
echo \Yiisoft\Html\Html::a('Yii Framework', 'https://yiiframework.cn/');
整体来说,辅助器有以下方法组。
创建标签对象
自定义标签
- tag
- normalTag
- voidTag
基本标签
- b
- div
- em
- i
- hr
- meta
- p
- br
- script
- noscript
- span
- strong
- small
- style
- title
媒体标签
- img
- picture
- audio
- video
- track
- source
标题标签
- h1
- h2
- h3
- h4
- h5
- h6
分区标签
- html
- body
- article
- section
- nav
- aside
- hgroup
- header
- footer
- address
列表标签
- ul
- ol
- li
超链接标签
- a
- mailto
链接标签
- link
- cssFile
- javaScriptFile
表单标签
- button
- buttonInput
- checkbox
- file
- datalist
- fieldset
- fileInput
- form
- hiddenInput
- input
- label
- legend
- optgroup
- option
- passwordInput
- radio
- resetButton
- resetInput
- select
- submitButton
- submitInput
- textInput
- textarea
表格标签
- table
- caption
- colgroup
- col
- thead
- tbody
- tfoot
- tr
- th
- td
生成标签部分
- openTag
- closeTag
- 渲染标签属性
创建HTML小部件对象
- radioList
- checkboxList
处理标签属性
- generateId
- getArrayableName
- getNonArrayableName
- normalizeRegexpPattern
编码和转义特殊字符
- encode
- encodeAttribute
- encodeUnquotedAttribute
- escapeJavaScriptStringValue
处理CSS样式和类
- addCssStyle
- removeCssStyle
- addCssClass
- removeCssClass
- cssStyleFromArray
- cssStyleToArray
文档
如果您需要帮助或有疑问,您可以访问Yii 论坛。您还可以查看其他Yii 社区资源。
许可
Yii HTML 是免费软件。它根据BSD许可证发布。有关更多信息,请参阅LICENSE。
由Yii 软件维护。