The package provides various tools to help with dynamic server-side generation of 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
(and specialized 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
class that helps to generate custom tag with any attributes.ButtonGroup
, CheckboxList
and RadioList
.NoEncode
class designed to wrap content that should not be encoded.Html
helper 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.
The package could be installed with 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.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 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 \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');
... will generate the following HTML:
<div id="ContactEmail" class="red"><a href="https://github.com/yiisoft/html/blob/master/mailto:info@example.com">contact us</a></div>
To generate custom tags, use the CustomTag
class. For example, the following code:
echo \Yiisoft\Html\Tag\CustomTag::name('b')
->content('text')
->attribute('title', 'Important');
... will generate the following HTML:
<b title="Important">text</b>
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\NoEncodeStringableInterface
interface 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>'));
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 \Yiisoft\Html\Widget\ButtonGroup::create()
->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 \Yiisoft\Html\Widget\CheckboxList\CheckboxList::create('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 \Yiisoft\Html\Widget\RadioList\RadioList::create('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 usageHtml
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.
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.
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.