0 follower

Final Class Yiisoft\Html\Html

InheritanceYiisoft\Html\Html

Html provides a set of static methods for generating commonly used HTML tags.

Nearly all the methods in this class allow setting additional HTML attributes for the HTML tags they generate. You can specify, for example, class, style or id for an HTML element using the $attributes parameter. See the documentation of the {@see \Yiisoft\Html\tag()} method for more details.

Public Methods

Hide inherited methods

Method Description Defined By
a() Generates a hyperlink tag. Yiisoft\Html\Html
addCssClass() Adds a CSS class (or several classes) to the specified attributes. Yiisoft\Html\Html
addCssStyle() Adds the specified CSS styles to the HTML attributes. Yiisoft\Html\Html
address() Generates a {@see Address} tag. Yiisoft\Html\Html
article() Generates a {@see Article} tag. Yiisoft\Html\Html
aside() Generates a {@see Aside} tag. Yiisoft\Html\Html
audio() Generates a {@see Audio} tag. Yiisoft\Html\Html
b() Generates a {@see B} tag. Yiisoft\Html\Html
body() Generates a {@see Body} tag. Yiisoft\Html\Html
br() Generates a {@see Br} tag. Yiisoft\Html\Html
button() Generates a button tag. Yiisoft\Html\Html
buttonInput() Generates an {@see Input} button. Yiisoft\Html\Html
caption() Generates a {@see Caption} tag. Yiisoft\Html\Html
checkbox() Generates a checkbox {@see Input}. Yiisoft\Html\Html
checkboxList() Generates a list of checkboxes. Yiisoft\Html\Html
closeTag() Generates an end tag. Yiisoft\Html\Html
code() Generates a {@see Code} tag. Yiisoft\Html\Html
col() Generates a {@see Col} tag. Yiisoft\Html\Html
colgroup() Generates a {@see Colgroup} tag. Yiisoft\Html\Html
color() Generates a color {@see Input} field. Yiisoft\Html\Html
cssFile() Generates a {@see Link} tag that refers to an CSS file. Yiisoft\Html\Html
cssStyleFromArray() Converts a CSS style array into a string representation. Yiisoft\Html\Html
cssStyleToArray() Converts a CSS style string into an array representation. Yiisoft\Html\Html
datalist() Generates a {@see Datalist} tag. Yiisoft\Html\Html
div() Generates a {@see Div} tag. Yiisoft\Html\Html
em() Generates a {@see Em} tag. Yiisoft\Html\Html
encode() Encodes special characters into HTML entities for use as a tag content i.e. <div>tag content</div>. Yiisoft\Html\Html
encodeAttribute() Encodes special characters into HTML entities for use as HTML tag quoted attribute value i.e. <input value="my-value">. Yiisoft\Html\Html
encodeUnquotedAttribute() Encodes special characters into HTML entities for use as HTML tag unquoted attribute value i.e. <input value=my-value>. Yiisoft\Html\Html
escapeJavaScriptStringValue() Escape special characters for use as JavaScript string value in a <script> tag: Yiisoft\Html\Html
fieldset() Generates a {@see Fieldset} tag. Yiisoft\Html\Html
file() Generates a file input field. Yiisoft\Html\Html
footer() Generates a {@see Footer} tag. Yiisoft\Html\Html
form() Generates a {@see Form} tag. Yiisoft\Html\Html
generateId() Generates a unique sequential ID composed of the given prefix, an hrtime(true) timestamp, and a counter that increments with each call sharing the same timestamp. Yiisoft\Html\Html
getArrayableName() Yiisoft\Html\Html
getNonArrayableName() Yiisoft\Html\Html
h1() Generates a {@see H1} tag. Yiisoft\Html\Html
h2() Generates a {@see H2} tag. Yiisoft\Html\Html
h3() Generates a {@see H3} tag. Yiisoft\Html\Html
h4() Generates a {@see H4} tag. Yiisoft\Html\Html
h5() Generates a {@see H5} tag. Yiisoft\Html\Html
h6() Generates a {@see H6} tag. Yiisoft\Html\Html
header() Generates a {@see Header} tag. Yiisoft\Html\Html
hgroup() Generates a {@see Hgroup} tag. Yiisoft\Html\Html
hiddenInput() Generates a hidden input field. Yiisoft\Html\Html
hr() Generates a {@see Hr} tag. Yiisoft\Html\Html
html() Generates a {@see Html} tag. Yiisoft\Html\Html
i() Generates a {@see I} tag. Yiisoft\Html\Html
img() Generates an {@see Img} tag. Yiisoft\Html\Html
input() Generates an {@see Input} type of the given type. Yiisoft\Html\Html
javaScriptFile() Generates a {@see Script} tag that refers to a JavaScript file. Yiisoft\Html\Html
label() Generates a {@see Label} tag. Yiisoft\Html\Html
legend() Generates a {@see Legend} tag. Yiisoft\Html\Html
li() Generates a {@see Li} tag. Yiisoft\Html\Html
link() Generates a {@see Link} tag. Yiisoft\Html\Html
mailto() Generates a mailto hyperlink tag. Yiisoft\Html\Html
meta() Generates a {@see Meta} tag. Yiisoft\Html\Html
normalTag() Generates a normal HTML tag. Yiisoft\Html\Html
normalizeRegexpPattern() Normalize PCRE regular expression to use in the "pattern" HTML attribute: - convert \x{FFFF} to \uFFFF; - remove flags and delimiters. Yiisoft\Html\Html
noscript() Generates a {@see Noscript} tag. Yiisoft\Html\Html
ol() Generates a {@see Ol} tag. Yiisoft\Html\Html
openTag() Generates a start tag. Yiisoft\Html\Html
optgroup() Generates a {@see Optgroup} tag. Yiisoft\Html\Html
option() Generates a {@see Option} tag. Yiisoft\Html\Html
p() Generates a {@see P} tag. Yiisoft\Html\Html
passwordInput() Generates a password input field. Yiisoft\Html\Html
picture() Generates a {@see Picture} tag. Yiisoft\Html\Html
pre() Generates a {@see Pre} tag. Yiisoft\Html\Html
radio() Generates a radio button {@see Input}. Yiisoft\Html\Html
radioList() Generates a list of radio buttons. Yiisoft\Html\Html
range() Generates a range {@see Range}. Yiisoft\Html\Html
removeCssClass() Removes a CSS class from the specified attributes. Yiisoft\Html\Html
removeCssStyle() Removes the specified CSS styles from the HTML attributes. Yiisoft\Html\Html
renderTagAttributes() Renders the HTML tag attributes. Yiisoft\Html\Html
resetButton() Generates a reset button tag. Yiisoft\Html\Html
resetInput() Generates a reset {@see Input} button. Yiisoft\Html\Html
script() Generates a {@see Script} tag. Yiisoft\Html\Html
section() Generates a {@see Section} tag. Yiisoft\Html\Html
select() Generates a {@see Select} tag. Yiisoft\Html\Html
small() Generates a {@see Small} tag. Yiisoft\Html\Html
source() Generates a {@see Source} tag. Yiisoft\Html\Html
span() Generates a {@see Span} tag. Yiisoft\Html\Html
strong() Generates a {@see Strong} tag. Yiisoft\Html\Html
style() Generates a {@see Style} tag. Yiisoft\Html\Html
submitButton() Generates a submit button tag. Yiisoft\Html\Html
submitInput() Generates submit {@see Input} button. Yiisoft\Html\Html
table() Generates a {@see Table} tag. Yiisoft\Html\Html
tag() Generates a complete HTML tag. Yiisoft\Html\Html
tbody() Generates a {@see Tbody} tag. Yiisoft\Html\Html
td() Generates a {@see Td} tag. Yiisoft\Html\Html
textInput() Generates a text {@see Input} field. Yiisoft\Html\Html
textarea() Generates a {@see Textarea} input. Yiisoft\Html\Html
tfoot() Generates a {@see Tfoot} tag. Yiisoft\Html\Html
th() Generates a {@see Th} tag. Yiisoft\Html\Html
thead() Generates a {@see Thead} tag. Yiisoft\Html\Html
title() Generates a {@see Title} tag. Yiisoft\Html\Html
tr() Generates a {@see Tr} tag. Yiisoft\Html\Html
track() Generates a {@see Track} tag. Yiisoft\Html\Html
ul() Generates a {@see Ul} tag. Yiisoft\Html\Html
video() Generates a {@see Video} tag. Yiisoft\Html\Html
voidTag() Generates a void HTML tag. Yiisoft\Html\Html

Constants

Hide inherited constants

Constant Value Description Defined By
ATTRIBUTES_WITH_CONCATENATED_VALUES [ 'class', 'aria-describedby', ] List of tag attributes which values should be concatenated with space. In particular, if the value of the attribute with the name attribute is ['value1', 'value2'], the result will be attribute="value1 value2". Yiisoft\Html\Html
DATA_ATTRIBUTES [ 'data', 'data-ng', 'ng', 'aria', ] List of tag attributes that should be specially handled when their values are of array type. In particular, if the value of the data attribute is ['name' => 'xyz', 'age' => 13], two attributes will be generated instead of one: data-name="xyz" data-age="13". Yiisoft\Html\Html

Method Details

Hide inherited methods

a() public static method

Generates a hyperlink tag.

See also Yiisoft\Html\Tag\A.

public static Yiisoft\Html\Tag\A a ( string|\Stringable|integer|float|null $content '', ?string $url null, array $attributes = [] )
$content string|\Stringable|integer|float|null

The tag content.

$url ?string
$attributes array

The tag attributes in terms of name-value pairs.

                public static function a(string|Stringable|int|float|null $content = '', ?string $url = null, array $attributes = []): A
{
    $tag = new A();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if ($url !== null) {
        $tag = $tag->url($url);
    }
    return $tag;
}

            
addCssClass() public static method

Adds a CSS class (or several classes) to the specified attributes.

If the CSS class is already in the attributes, it will not be added again. If class specification at given attributes is an array, and some class placed there with the named (string) key, overriding of such key will have no effect. For example:

$attributes = ['class' => ['persistent' => 'initial']];

// ['class' => ['persistent' => 'initial']];
Html::addCssClass($attributes, ['persistent' => 'override']);

See also removeCssClass().

public static void addCssClass ( array &$attributes, \BackedEnum|\BackedEnum[]|null[]|string|string[]|null $class )
$attributes array

The attributes to be modified. All string values in the array must be valid UTF-8 strings.

$class \BackedEnum|\BackedEnum[]|null[]|string|string[]|null

The CSS class(es) to be added. Null values will be ignored.

                public static function addCssClass(array &$attributes, BackedEnum|array|string|null $class): void
{
    if ($class === null) {
        return;
    }
    if ($class instanceof BackedEnum) {
        if (is_int($class->value)) {
            return;
        }
        $class = $class->value;
    }
    if (is_array($class)) {
        $filteredClass = [];
        foreach ($class as $key => $value) {
            if ($value instanceof BackedEnum) {
                $value = is_string($value->value) ? $value->value : null;
            }
            if ($value !== null) {
                $filteredClass[$key] = $value;
            }
        }
        if (empty($filteredClass)) {
            return;
        }
        $class = $filteredClass;
    }
    if (isset($attributes['class'])) {
        if (is_array($attributes['class'])) {
            /** @psalm-var string[] $attributes['class'] */
            $attributes['class'] = self::mergeCssClasses($attributes['class'], (array) $class);
        } else {
            /**
             * @psalm-var string $attributes['class']
             * @var string[] $classes We assume that `$attributes['class']` is valid UTF-8 string, so `preg_split()`
             * never returns `false`.
             */
            $classes = preg_split('/\s+/', $attributes['class'], -1, PREG_SPLIT_NO_EMPTY);
            $classes = self::mergeCssClasses($classes, (array) $class);
            $attributes['class'] = is_array($class) ? $classes : implode(' ', $classes);
        }
    } else {
        $attributes['class'] = $class;
    }
}

            
addCssStyle() public static method

Adds the specified CSS styles to the HTML attributes.

If the attributes already contain a style element, the new style will be merged with the existing one. If a CSS property exists in both the new and the old styles, the old one may be overwritten if $overwrite is true.

For example,

Html::addCssStyle($attributes, 'width: 100px; height: 200px');

See also removeCssStyle().

public static void addCssStyle ( array &$attributes, string|string[] $style, boolean $overwrite true )
$attributes array

The HTML attributes to be modified.

$style string|string[]

The new style string (e.g. 'width: 100px; height: 200px') or array (e.g. ['width' => '100px', 'height' => '200px']).

$overwrite boolean

Whether to overwrite existing CSS properties if the new style contains them too.

                public static function addCssStyle(array &$attributes, array|string $style, bool $overwrite = true): void
{
    if (!empty($attributes['style'])) {
        /** @psalm-var array<string,string>|string $attributes['style'] */
        $oldStyle = is_array($attributes['style']) ? $attributes['style'] : self::cssStyleToArray($attributes['style']);
        $newStyle = is_array($style) ? $style : self::cssStyleToArray($style);
        if (!$overwrite) {
            foreach ($newStyle as $property => $_value) {
                if (isset($oldStyle[$property])) {
                    unset($newStyle[$property]);
                }
            }
        }
        $style = array_merge($oldStyle, $newStyle);
    }
    $attributes['style'] = is_array($style) ? self::cssStyleFromArray($style) : $style;
}

            
address() public static method

Generates a {@see Address} tag.

public static Yiisoft\Html\Tag\Address address ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function address(string|Stringable|int|float|null $content = '', array $attributes = []): Address
{
    $tag = new Address();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
article() public static method

Generates a {@see Article} tag.

public static Yiisoft\Html\Tag\Article article ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function article(string|Stringable|int|float|null $content = '', array $attributes = []): Article
{
    $tag = new Article();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
aside() public static method

Generates a {@see Aside} tag.

public static Yiisoft\Html\Tag\Aside aside ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function aside(string|Stringable|int|float|null $content = '', array $attributes = []): Aside
{
    $tag = new Aside();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
audio() public static method

Generates a {@see Audio} tag.

public static Yiisoft\Html\Tag\Audio audio ( )

                public static function audio(): Audio
{
    return new Audio();
}

            
b() public static method

Generates a {@see B} tag.

public static Yiisoft\Html\Tag\B b ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function b(string|Stringable|int|float|null $content = '', array $attributes = []): B
{
    $tag = new B();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
body() public static method

Generates a {@see Body} tag.

public static Yiisoft\Html\Tag\Body body ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function body(string|Stringable|int|float|null $content = '', array $attributes = []): Body
{
    $tag = new Body();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
br() public static method

Generates a {@see Br} tag.

public static Yiisoft\Html\Tag\Br br ( )

                public static function br(): Br
{
    return new Br();
}

            
button() public static method

Generates a button tag.

See also Yiisoft\Html\Tag\Button::button().

public static Yiisoft\Html\Tag\Button button ( string|\Stringable|integer|float|null $content 'Button', array $attributes = [] )
$content string|\Stringable|integer|float|null

The content enclosed within the button tag.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function button(string|Stringable|int|float|null $content = 'Button', array $attributes = []): Button
{
    $tag = Button::button($content);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
buttonInput() public static method

Generates an {@see Input} button.

See also Yiisoft\Html\Tag\Input::button().

public static Yiisoft\Html\Tag\Input buttonInput ( string|null $label 'Button', array $attributes = [] )
$label string|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function buttonInput(?string $label = 'Button', array $attributes = []): Input
{
    $tag = Input::button($label);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
caption() public static method

Generates a {@see Caption} tag.

public static Yiisoft\Html\Tag\Caption caption ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function caption(string|Stringable|int|float|null $content = '', array $attributes = []): Caption
{
    $tag = new Caption();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if ($attributes !== []) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
checkbox() public static method

Generates a checkbox {@see Input}.

See also Yiisoft\Html\Tag\Input::checkbox().

public static Yiisoft\Html\Tag\Input\Checkbox checkbox ( string|null $name null, boolean|float|integer|string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value boolean|float|integer|string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function checkbox(
    ?string $name = null,
    bool|float|int|string|Stringable|null $value = null,
    array $attributes = [],
): Checkbox {
    $tag = Input::checkbox($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
checkboxList() public static method

Generates a list of checkboxes.

See also Yiisoft\Html\Widget\CheckboxList\CheckboxList.

public static Yiisoft\Html\Widget\CheckboxList\CheckboxList checkboxList ( string $name )
$name string

                public static function checkboxList(string $name): CheckboxList
{
    return new CheckboxList($name);
}

            
closeTag() public static method

Generates an end tag.

See also \Yiisoft\Html\self::openTag().

public static string closeTag ( string $name )
$name string

The tag name.

                public static function closeTag(string $name): string
{
    return "</$name>";
}

            
code() public static method

Generates a {@see Code} tag.

public static Yiisoft\Html\Tag\Code code ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content. If content includes HTML tags or special characters like <, >, or &, and you want them to be displayed as plain text, you need to enable encoding by calling ->encode(true). This ensures they are not interpreted as actual HTML.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function code(string|Stringable|int|float|null $content = '', array $attributes = []): Code
{
    $tag = new Code();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
col() public static method

Generates a {@see Col} tag.

public static Yiisoft\Html\Tag\Col col ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function col(array $attributes = []): Col
{
    $tag = new Col();
    return $attributes === [] ? $tag : $tag->attributes($attributes);
}

            
colgroup() public static method

Generates a {@see Colgroup} tag.

public static Yiisoft\Html\Tag\Colgroup colgroup ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function colgroup(array $attributes = []): Colgroup
{
    $tag = new Colgroup();
    return $attributes === [] ? $tag : $tag->attributes($attributes);
}

            
color() public static method

Generates a color {@see Input} field.

See also Yiisoft\Html\Tag\Input::color().

public static Yiisoft\Html\Tag\Input\Color color ( string|null $name null, string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function color(
    ?string $name = null,
    string|Stringable|null $value = null,
    array $attributes = [],
): Color {
    $tag = Input::color($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
cssFile() public static method

Generates a {@see Link} tag that refers to an CSS file.

public static Yiisoft\Html\Tag\Link cssFile ( string $url, array $attributes = [] )
$url string

The URL of the CSS file.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function cssFile(string $url, array $attributes = []): Link
{
    $tag = Link::toCssFile($url);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
cssStyleFromArray() public static method

Converts a CSS style array into a string representation.

For example,

// width: 100px; height: 200px;
Html::cssStyleFromArray(['width' => '100px', 'height' => '200px']);

See also cssStyleToArray().

public static string|null cssStyleFromArray ( array $style )
$style array

The CSS style array. The array keys are the CSS property names, and the array values are the corresponding CSS property values.

return string|null

The CSS style string. If the CSS style is empty, a null will be returned.

                public static function cssStyleFromArray(array $style): ?string
{
    $result = '';
    foreach ($style as $name => $value) {
        $result .= "$name: $value; ";
    }
    // Return null if empty to avoid rendering the "style" attribute.
    return $result === '' ? null : rtrim($result);
}

            
cssStyleToArray() public static method

Converts a CSS style string into an array representation.

The array keys are the CSS property names, and the array values are the corresponding CSS property values.

For example,

// ['width' => '100px', 'height' => '200px']
Html::cssStyleToArray('width: 100px; height: 200px;');

See also cssStyleFromArray().

public static array cssStyleToArray ( string|\Stringable $style )
$style string|\Stringable

The CSS style string.

return array

The array representation of the CSS style.

                public static function cssStyleToArray(string|Stringable $style): array
{
    $result = [];
    foreach (explode(';', (string) $style) as $property) {
        $property = explode(':', $property);
        if (count($property) > 1) {
            $result[trim($property[0])] = trim($property[1]);
        }
    }
    return $result;
}

            
datalist() public static method

Generates a {@see Datalist} tag.

public static Yiisoft\Html\Tag\Datalist datalist ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function datalist(array $attributes = []): Datalist
{
    $tag = new Datalist();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
div() public static method

Generates a {@see Div} tag.

public static Yiisoft\Html\Tag\Div div ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function div(string|Stringable|int|float|null $content = '', array $attributes = []): Div
{
    $tag = new Div();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
em() public static method

Generates a {@see Em} tag.

public static Yiisoft\Html\Tag\Em em ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function em(string|Stringable|int|float|null $content = '', array $attributes = []): Em
{
    $tag = new Em();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
encode() public static method

Encodes special characters into HTML entities for use as a tag content i.e. <div>tag content</div>.

Characters encoded are: &, <, >.

public static string encode ( mixed $content, boolean $doubleEncode true, string $encoding 'UTF-8' )
$content mixed

The content to be encoded.

$doubleEncode boolean

If already encoded entities should be encoded.

$encoding string

The encoding to use, defaults to "UTF-8".

return string

Encoded content.

                public static function encode(mixed $content, bool $doubleEncode = true, string $encoding = 'UTF-8'): string
{
    return htmlspecialchars(
        (string) $content,
        ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML5,
        $encoding,
        $doubleEncode,
    );
}

            
encodeAttribute() public static method

Encodes special characters into HTML entities for use as HTML tag quoted attribute value i.e. <input value="my-value">.

Characters encoded are: &, <, >, ", ', U+0000 (null).

public static string encodeAttribute ( mixed $value, boolean $doubleEncode true, string $encoding 'UTF-8' )
$value mixed

The attribute value to be encoded.

$doubleEncode boolean

If already encoded entities should be encoded.

$encoding string

The encoding to use, defaults to "UTF-8".

return string

Encoded attribute value.

                public static function encodeAttribute(mixed $value, bool $doubleEncode = true, string $encoding = 'UTF-8'): string
{
    $value = htmlspecialchars(
        (string) $value,
        ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5,
        $encoding,
        $doubleEncode,
    );
    return strtr($value, [
        "\u{0000}" => '&#0;', // U+0000 NULL
    ]);
}

            
encodeUnquotedAttribute() public static method

Encodes special characters into HTML entities for use as HTML tag unquoted attribute value i.e. <input value=my-value>.

Characters encoded are: &, <, >, ", ', `, =, tab, space, U+000A (form feed), U+0000 (null).

public static string encodeUnquotedAttribute ( mixed $value, boolean $doubleEncode true, string $encoding 'UTF-8' )
$value mixed

The attribute value to be encoded.

$doubleEncode boolean

If already encoded entities should be encoded.

$encoding string

The encoding to use, defaults to "UTF-8".

return string

Encoded attribute value.

                public static function encodeUnquotedAttribute(
    mixed $value,
    bool $doubleEncode = true,
    string $encoding = 'UTF-8',
): string {
    $value = htmlspecialchars(
        (string) $value,
        ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5,
        $encoding,
        $doubleEncode,
    );
    return strtr($value, [
        "\t" => '&Tab;', // U+0009 CHARACTER TABULATION (tab)
        "\n" => '&NewLine;', // U+000A LINE FEED (LF)
        "\u{000c}" => '&#12;', // U+000C FORM FEED (FF)
        "\u{0000}" => '&#0;', // U+0000 NULL
        ' ' => '&#32;', // U+0020 SPACE
        '=' => '&equals;', // U+003D EQUALS SIGN (=)
        '`' => '&grave;', // U+0060 GRAVE ACCENT (`)
    ]);
}

            
escapeJavaScriptStringValue() public static method

Escape special characters for use as JavaScript string value in a <script> tag:

<script>
    window.myVar = "<?= Html::escapeJavaScriptStringValue($myVar) ?>";
</script>
public static string escapeJavaScriptStringValue ( mixed $value )
$value mixed

JavaScript string.

return string

Escaped JavaScript string.

                public static function escapeJavaScriptStringValue(mixed $value): string
{
    return strtr((string) $value, [
        '/' => '\/',
        '"' => '\"',
        "'" => "\'",
        '\\' => '\\\\',
    ]);
}

            
fieldset() public static method

Generates a {@see Fieldset} tag.

public static Yiisoft\Html\Tag\Fieldset fieldset ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function fieldset(array $attributes = []): Fieldset
{
    $tag = new Fieldset();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
file() public static method

Generates a file input field.

To use a file input field, you should set the enclosing form's "enctype" attribute to be "multipart/form-data". After the form is submitted, the uploaded file information can be obtained via $_FILES[$name] (see PHP documentation).

See also Yiisoft\Html\Tag\Input::file().

public static Yiisoft\Html\Tag\Input\File file ( string|null $name null, boolean|float|integer|string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value boolean|float|integer|string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function file(
    ?string $name = null,
    bool|float|int|string|Stringable|null $value = null,
    array $attributes = [],
): File {
    $tag = Input::file($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
footer() public static method

Generates a {@see Footer} tag.

public static Yiisoft\Html\Tag\Footer footer ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function footer(string|Stringable|int|float|null $content = '', array $attributes = []): Footer
{
    $tag = new Footer();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
form() public static method

Generates a {@see Form} tag.

public static Yiisoft\Html\Tag\Form form ( string|null $action null, string|null $method null, array $attributes = [] )
$action string|null

The URL to use for form submission.

$method string|null

The method attribute value.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function form(?string $action = null, ?string $method = null, array $attributes = []): Form
{
    $tag = new Form();
    if ($action !== null) {
        $attributes['action'] = $action;
    }
    if ($method !== null) {
        $attributes['method'] = $method;
    }
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
generateId() public static method

Generates a unique sequential ID composed of the given prefix, an hrtime(true) timestamp, and a counter that increments with each call sharing the same timestamp.

See also https://github.com/yiisoft/html/blob/master/docs/testing-id-generation.md for controlling ID generation in tests.

public static string generateId ( string $prefix 'i' )
$prefix string

Prefix to prepend to the generated ID.

return string

Autogenerated ID.

                public static function generateId(string $prefix = 'i'): string
{
    return IdGenerator::generate($prefix);
}

            
getArrayableName() public static method

public static string getArrayableName ( string $name )
$name string

                public static function getArrayableName(string $name): string
{
    return !str_ends_with($name, '[]') ? $name . '[]' : $name;
}

            
getNonArrayableName() public static method

public static string getNonArrayableName ( string $name )
$name string

                public static function getNonArrayableName(string $name): string
{
    return str_ends_with($name, '[]') ? substr($name, 0, -2) : $name;
}

            
h1() public static method

Generates a {@see H1} tag.

public static Yiisoft\Html\Tag\H1 h1 ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function h1(string|Stringable|int|float|null $content = '', array $attributes = []): H1
{
    $tag = new H1();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
h2() public static method

Generates a {@see H2} tag.

public static Yiisoft\Html\Tag\H2 h2 ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function h2(string|Stringable|int|float|null $content = '', array $attributes = []): H2
{
    $tag = new H2();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
h3() public static method

Generates a {@see H3} tag.

public static Yiisoft\Html\Tag\H3 h3 ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function h3(string|Stringable|int|float|null $content = '', array $attributes = []): H3
{
    $tag = new H3();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
h4() public static method

Generates a {@see H4} tag.

public static Yiisoft\Html\Tag\H4 h4 ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function h4(string|Stringable|int|float|null $content = '', array $attributes = []): H4
{
    $tag = new H4();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
h5() public static method

Generates a {@see H5} tag.

public static Yiisoft\Html\Tag\H5 h5 ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function h5(string|Stringable|int|float|null $content = '', array $attributes = []): H5
{
    $tag = new H5();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
h6() public static method

Generates a {@see H6} tag.

public static Yiisoft\Html\Tag\H6 h6 ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function h6(string|Stringable|int|float|null $content = '', array $attributes = []): H6
{
    $tag = new H6();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
header() public static method

Generates a {@see Header} tag.

public static Yiisoft\Html\Tag\Header header ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function header(string|Stringable|int|float|null $content = '', array $attributes = []): Header
{
    $tag = new Header();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
hgroup() public static method

Generates a {@see Hgroup} tag.

public static Yiisoft\Html\Tag\Hgroup hgroup ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function hgroup(string|Stringable|int|float|null $content = '', array $attributes = []): Hgroup
{
    $tag = new Hgroup();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
hiddenInput() public static method

Generates a hidden input field.

See also Yiisoft\Html\Tag\Input::hidden().

public static Yiisoft\Html\Tag\Input hiddenInput ( string|null $name null, boolean|float|integer|string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value boolean|float|integer|string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function hiddenInput(
    ?string $name = null,
    bool|float|int|string|Stringable|null $value = null,
    array $attributes = [],
): Input {
    $tag = Input::hidden($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
hr() public static method

Generates a {@see Hr} tag.

public static Yiisoft\Html\Tag\Hr hr ( array $attributes = [] )
$attributes array

                public static function hr(array $attributes = []): Hr
{
    $tag = new Hr();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
html() public static method

Generates a {@see Html} tag.

public static Yiisoft\Html\Tag\Html html ( string|\Stringable|integer|float|null $content '', string|null $lang null, array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$lang string|null

The document language.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function html(string|Stringable|int|float|null $content = '', ?string $lang = null, array $attributes = []): Tag\Html
{
    $tag = new Tag\Html();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if ($lang !== null) {
        $tag = $tag->lang($lang);
    }
    return $tag;
}

            
i() public static method

Generates a {@see I} tag.

public static Yiisoft\Html\Tag\I i ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function i(string|Stringable|int|float|null $content = '', array $attributes = []): I
{
    $tag = new I();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
img() public static method

Generates an {@see Img} tag.

public static Yiisoft\Html\Tag\Img img ( string|null $url null, string|null $alt '', array $attributes = [] )
$url string|null

The image URL.

$alt string|null

Alt text.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function img(?string $url = null, ?string $alt = '', array $attributes = []): Img
{
    $tag = new Img();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    if ($url !== null) {
        $tag = $tag->src($url);
    }
    if ($alt !== null) {
        $tag = $tag->alt($alt);
    }
    return $tag;
}

            
input() public static method

Generates an {@see Input} type of the given type.

public static Yiisoft\Html\Tag\Input input ( string $type, string|null $name null, boolean|float|integer|string|\Stringable|null $value null, array $attributes = [] )
$type string

The type attribute.

$name string|null

The name attribute. If it is null, the name attribute will not be generated.

$value boolean|float|integer|string|\Stringable|null

The value attribute. If it is null, the value attribute will not be generated.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function input(
    string $type,
    ?string $name = null,
    bool|float|int|string|Stringable|null $value = null,
    array $attributes = [],
): Input {
    $tag = (new Input())->type($type);
    if ($name !== null) {
        $tag = $tag->name($name);
    }
    if ($value !== null) {
        $tag = $tag->value($value);
    }
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
javaScriptFile() public static method

Generates a {@see Script} tag that refers to a JavaScript file.

public static Yiisoft\Html\Tag\Script javaScriptFile ( string $url, array $attributes = [] )
$url string

The URL of the JavaScript file.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function javaScriptFile(string $url, array $attributes = []): Script
{
    $tag = (new Script())->url($url);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
label() public static method

Generates a {@see Label} tag.

public static Yiisoft\Html\Tag\Label label ( string|\Stringable|integer|float|null $content '', string|null $for null )
$content string|\Stringable|integer|float|null

Label text.

$for string|null

The ID of the HTML element that this label is associated with. If this is null, the "for" attribute will not be generated.

                public static function label(string|Stringable|int|float|null $content = '', ?string $for = null): Label
{
    $tag = new Label();
    if ($for !== null) {
        $tag = $tag->forId($for);
    }
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    return $tag;
}

            
legend() public static method

Generates a {@see Legend} tag.

public static Yiisoft\Html\Tag\Legend legend ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

The tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function legend(string|Stringable|int|float|null $content = '', array $attributes = []): Legend
{
    $tag = new Legend();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
li() public static method

Generates a {@see Li} tag.

public static Yiisoft\Html\Tag\Li li ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function li(string|Stringable|int|float|null $content = '', array $attributes = []): Li
{
    $tag = new Li();
    if ($attributes !== []) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
link() public static method

Generates a {@see Link} tag.

public static Yiisoft\Html\Tag\Link link ( string|null $url null, array $attributes = [] )
$url string|null

The destination of the link.

$attributes array

The tag attributes in terms of name-value pairs.

mailto() public static method

Generates a mailto hyperlink tag.

See also Yiisoft\Html\Tag\A.

public static Yiisoft\Html\Tag\A mailto ( string|\Stringable|integer|float|null $content, ?string $mail null, array $attributes = [] )
$content string|\Stringable|integer|float|null
$mail ?string
$attributes array

The tag attributes in terms of name-value pairs.

                public static function mailto(string|Stringable|int|float|null $content, ?string $mail = null, array $attributes = []): A
{
    $tag = (new A())
        ->content($content)
        ->mailto($mail ?? ($content === null ? null : (string) $content));
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
meta() public static method

Generates a {@see Meta} tag.

public static Yiisoft\Html\Tag\Meta meta ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function meta(array $attributes = []): Meta
{
    $tag = new Meta();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
normalTag() public static method

Generates a normal HTML tag.

See also Yiisoft\Html\Tag\CustomTag.

public static Yiisoft\Html\Tag\CustomTag normalTag ( string $name, string|\Stringable|integer|float|null $content '', array $attributes = [] )
$name string

The tag name.

$content string|\Stringable|integer|float|null

The tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function normalTag(string $name, string|Stringable|int|float|null $content = '', array $attributes = []): CustomTag
{
    $tag = (new CustomTag($name))->normal();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
normalizeRegexpPattern() public static method

Normalize PCRE regular expression to use in the "pattern" HTML attribute: - convert \x{FFFF} to \uFFFF; - remove flags and delimiters.

For example:

Html::normalizeRegexpPattern('/([a-z0-9-]+)/im'); // will return: ([a-z0-9-]+)
public static string normalizeRegexpPattern ( string $regexp, string|null $delimiter null )
$regexp string

PCRE regular expression. It must be valid UTF-8 string.

$delimiter string|null

Regular expression delimiter.

return string

Value for use in the "pattern" HTML attribute

throws InvalidArgumentException

if incorrect regular expression or delimiter

                public static function normalizeRegexpPattern(string $regexp, ?string $delimiter = null): string
{
    if (strlen($regexp) < 2) {
        throw new InvalidArgumentException('Incorrect regular expression.');
    }
    /**
     * @var string $pattern We assume that `$regexp` is valid UTF-8 string, so `preg_replace()` never returns
     * `null`.
     */
    $pattern = preg_replace('/\\\\x{?([0-9a-fA-F]+)}?/', '\u$1', $regexp);
    if ($delimiter === null) {
        $delimiter = substr($pattern, 0, 1);
    } elseif (strlen($delimiter) !== 1) {
        throw new InvalidArgumentException('Incorrect delimiter.');
    }
    $endPosition = strrpos($pattern, $delimiter, 1);
    if ($endPosition === false) {
        throw new InvalidArgumentException('Incorrect regular expression.');
    }
    return substr($pattern, 1, $endPosition - 1);
}

            
noscript() public static method

Generates a {@see Noscript} tag.

public static Yiisoft\Html\Tag\Noscript noscript ( string|\Stringable|integer|float|null $content '' )
$content string|\Stringable|integer|float|null

Tag content.

                public static function noscript(string|Stringable|int|float|null $content = ''): Noscript
{
    $tag = new Noscript();
    return $content === '' ? $tag : $tag->content($content);
}

            
ol() public static method

Generates a {@see Ol} tag.

public static Yiisoft\Html\Tag\Ol ol ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function ol(array $attributes = []): Ol
{
    $tag = new Ol();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
openTag() public static method

Generates a start tag.

See also \Yiisoft\Html\self::closeTag().

public static string openTag ( string $name, array $attributes = [] )
$name string

The tag name.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function openTag(string $name, array $attributes = []): string
{
    return '<' . $name . self::renderTagAttributes($attributes) . '>';
}

            
optgroup() public static method

Generates a {@see Optgroup} tag.

public static Yiisoft\Html\Tag\Optgroup optgroup ( )

                public static function optgroup(): Optgroup
{
    return new Optgroup();
}

            
option() public static method

Generates a {@see Option} tag.

public static Yiisoft\Html\Tag\Option option ( string|\Stringable|integer|float|null $content '', boolean|float|integer|string|\Stringable|null $value null )
$content string|\Stringable|integer|float|null

Tag content.

$value boolean|float|integer|string|\Stringable|null

The value attribute.

                public static function option(
    string|Stringable|int|float|null $content = '',
    bool|float|int|string|Stringable|null $value = null,
): Option {
    $tag = new Option();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if ($value !== null) {
        $tag = $tag->value($value);
    }
    return $tag;
}

            
p() public static method

Generates a {@see P} tag.

public static Yiisoft\Html\Tag\P p ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function p(string|Stringable|int|float|null $content = '', array $attributes = []): P
{
    $tag = new P();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
passwordInput() public static method

Generates a password input field.

See also Yiisoft\Html\Tag\Input::password().

public static Yiisoft\Html\Tag\Input passwordInput ( string|null $name null, boolean|float|integer|string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value boolean|float|integer|string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function passwordInput(
    ?string $name = null,
    bool|float|int|string|Stringable|null $value = null,
    array $attributes = [],
): Input {
    $tag = Input::password($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
picture() public static method

Generates a {@see Picture} tag.

public static Yiisoft\Html\Tag\Picture picture ( )

                public static function picture(): Picture
{
    return new Picture();
}

            
pre() public static method

Generates a {@see Pre} tag.

public static Yiisoft\Html\Tag\Pre pre ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content. If content includes HTML tags or special characters like <, >, or &, and you want them to be displayed as plain text, you need to enable encoding by calling ->encode(true). This ensures they are not interpreted as actual HTML.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function pre(string|Stringable|int|float|null $content = '', array $attributes = []): Pre
{
    $tag = new Pre();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
radio() public static method

Generates a radio button {@see Input}.

See also Yiisoft\Html\Tag\Input::radio().

public static Yiisoft\Html\Tag\Input\Radio radio ( string|null $name null, boolean|float|integer|string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value boolean|float|integer|string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function radio(
    ?string $name = null,
    bool|float|int|string|Stringable|null $value = null,
    array $attributes = [],
): Radio {
    $tag = Input::radio($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
radioList() public static method

Generates a list of radio buttons.

See also Yiisoft\Html\Widget\RadioList\RadioList.

public static Yiisoft\Html\Widget\RadioList\RadioList radioList ( string $name )
$name string

                public static function radioList(string $name): RadioList
{
    return new RadioList($name);
}

            
range() public static method

Generates a range {@see Range}.

See also Yiisoft\Html\Tag\Input::range().

public static Yiisoft\Html\Tag\Input\Range range ( string|null $name null, float|integer|string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value float|integer|string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function range(
    ?string $name = null,
    float|int|string|Stringable|null $value = null,
    array $attributes = [],
): Range {
    $tag = Input::range($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
removeCssClass() public static method

Removes a CSS class from the specified attributes.

See also addCssClass().

public static void removeCssClass ( array &$attributes, string|string[] $class )
$attributes array

The attributes to be modified.

$class string|string[]

The CSS class(es) to be removed.

                public static function removeCssClass(array &$attributes, string|array $class): void
{
    if (isset($attributes['class'])) {
        if (is_array($attributes['class'])) {
            $classes = array_diff($attributes['class'], (array) $class);
            if (empty($classes)) {
                unset($attributes['class']);
            } else {
                $attributes['class'] = $classes;
            }
        } else {
            /** @var string[] */
            $classes = preg_split('/\s+/', (string) $attributes['class'], -1, PREG_SPLIT_NO_EMPTY);
            $classes = array_diff($classes, (array) $class);
            if (empty($classes)) {
                unset($attributes['class']);
            } else {
                $attributes['class'] = implode(' ', $classes);
            }
        }
    }
}

            
removeCssStyle() public static method

Removes the specified CSS styles from the HTML attributes.

For example,

Html::removeCssStyle($attributes, ['width', 'height']);

See also addCssStyle().

public static void removeCssStyle ( array &$attributes, string|string[] $properties )
$attributes array

The HTML attributes to be modified.

$properties string|string[]

The CSS properties to be removed. You may use a string if you are removing a single property.

                public static function removeCssStyle(array &$attributes, string|array $properties): void
{
    if (!empty($attributes['style'])) {
        /** @psalm-var array<string,string>|string $attributes['style'] */
        $style = is_array($attributes['style']) ? $attributes['style'] : self::cssStyleToArray($attributes['style']);
        foreach ((array) $properties as $property) {
            unset($style[$property]);
        }
        $attributes['style'] = self::cssStyleFromArray($style);
    }
}

            
renderTagAttributes() public static method

Renders the HTML tag attributes.

Attributes whose values are of boolean type will be treated as boolean attributes.

Attributes whose values are null will not be rendered. The values of attributes will be HTML-encoded using {@see \Yiisoft\Html\encodeAttribute()}.

The "data" attribute is specially handled when it is receiving an array value. In this case, the array will be "expanded" and a list data attributes will be rendered. For example, if 'data' => ['id' => 1, 'name' => 'yii'] then this will be rendered data-id="1" data-name="yii".

Additionally, 'data' => ['params' => ['id' => 1, 'name' => 'yii'], 'status' => 'ok'] will be rendered as: data-params='{"id":1,"name":"yii"}' data-status="ok".

public static string renderTagAttributes ( array $attributes )
$attributes array

Attributes to be rendered. The attribute values will be HTML-encoded using {@see \Yiisoft\Html\encodeAttribute()}.

return string

The rendering result. If the attributes are not empty, they will be rendered into a string with a leading white space (so that it can be directly appended to the tag name in a tag). If there is no attribute, an empty string will be returned.

throws JsonException

                public static function renderTagAttributes(array $attributes): string
{
    $html = '';
    /**
     * @var string $name
     */
    foreach ($attributes as $name => $value) {
        if (is_bool($value)) {
            if ($value) {
                $html .= self::renderAttribute($name);
            }
        } elseif (is_array($value)) {
            if (in_array($name, self::DATA_ATTRIBUTES, true)) {
                /** @psalm-var array<array-key, scalar[]|string|Stringable|null> $value */
                foreach ($value as $n => $v) {
                    if (!isset($v)) {
                        continue;
                    }
                    $fullName = "$name-$n";
                    if (in_array($fullName, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) {
                        $html .= self::renderAttribute(
                            $fullName,
                            self::encodeAttribute(
                                is_array($v) ? implode(' ', $v) : $v,
                            ),
                        );
                    } else {
                        $html .= is_array($v)
                            ? self::renderAttribute($fullName, Json::htmlEncode($v), '\'')
                            : self::renderAttribute($fullName, self::encodeAttribute($v));
                    }
                }
            } elseif (in_array($name, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) {
                /** @var string[] $value */
                if (empty($value)) {
                    continue;
                }
                $html .= self::renderAttribute($name, self::encodeAttribute(implode(' ', $value)));
            } elseif ($name === 'style') {
                /** @psalm-var array<string,string> $value */
                if (empty($value)) {
                    continue;
                }
                $html .= self::renderAttribute($name, self::encodeAttribute(self::cssStyleFromArray($value)));
            } else {
                $html .= self::renderAttribute($name, Json::htmlEncode($value), '\'');
            }
        } elseif ($value !== null) {
            $html .= self::renderAttribute($name, self::encodeAttribute($value));
        }
    }
    return $html;
}

            
resetButton() public static method

Generates a reset button tag.

See also Yiisoft\Html\Tag\Button::reset().

public static Yiisoft\Html\Tag\Button resetButton ( string|\Stringable|integer|float|null $content 'Reset', array $attributes = [] )
$content string|\Stringable|integer|float|null

The content enclosed within the button tag.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function resetButton(string|Stringable|int|float|null $content = 'Reset', array $attributes = []): Button
{
    $tag = Button::reset($content);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
resetInput() public static method

Generates a reset {@see Input} button.

See also Yiisoft\Html\Tag\Input::resetButton().

public static Yiisoft\Html\Tag\Input resetInput ( string|null $label 'Reset', array $attributes = [] )
$label string|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function resetInput(?string $label = 'Reset', array $attributes = []): Input
{
    $tag = Input::resetButton($label);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
script() public static method

Generates a {@see Script} tag.

public static Yiisoft\Html\Tag\Script script ( string|\Stringable $content '', array $attributes = [] )
$content string|\Stringable

The script content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function script(string|Stringable $content = '', array $attributes = []): Script
{
    $tag = new Script();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
section() public static method

Generates a {@see Section} tag.

public static Yiisoft\Html\Tag\Section section ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function section(string|Stringable|int|float|null $content = '', array $attributes = []): Section
{
    $tag = new Section();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
select() public static method

Generates a {@see Select} tag.

public static Yiisoft\Html\Tag\Select select ( string|null $name null )
$name string|null

The name attribute

                public static function select(?string $name = null): Select
{
    $tag = new Select();
    if ($name !== null) {
        $tag = $tag->name($name);
    }
    return $tag;
}

            
small() public static method

Generates a {@see Small} tag.

public static Yiisoft\Html\Tag\Small small ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function small(string|Stringable|int|float|null $content = '', array $attributes = []): Small
{
    $tag = new Small();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
source() public static method

Generates a {@see Source} tag.

public static Yiisoft\Html\Tag\Source source ( )

                public static function source(): Source
{
    return new Source();
}

            
span() public static method

Generates a {@see Span} tag.

public static Yiisoft\Html\Tag\Span span ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function span(string|Stringable|int|float|null $content = '', array $attributes = []): Span
{
    $tag = new Span();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
strong() public static method

Generates a {@see Strong} tag.

public static Yiisoft\Html\Tag\Strong strong ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function strong(string|Stringable|int|float|null $content = '', array $attributes = []): Strong
{
    $tag = new Strong();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
style() public static method

Generates a {@see Style} tag.

public static Yiisoft\Html\Tag\Style style ( string|\Stringable $content '', array $attributes = [] )
$content string|\Stringable

The style content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function style(string|Stringable $content = '', array $attributes = []): Style
{
    $tag = new Style();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
submitButton() public static method

Generates a submit button tag.

See also Yiisoft\Html\Tag\Button::submit().

public static Yiisoft\Html\Tag\Button submitButton ( string|\Stringable|integer|float|null $content 'Submit', array $attributes = [] )
$content string|\Stringable|integer|float|null

The content enclosed within the button tag.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function submitButton(string|Stringable|int|float|null $content = 'Submit', array $attributes = []): Button
{
    $tag = Button::submit($content);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
submitInput() public static method

Generates submit {@see Input} button.

See also Yiisoft\Html\Tag\Input::submitButton().

public static Yiisoft\Html\Tag\Input submitInput ( string|null $label 'Submit', array $attributes = [] )
$label string|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function submitInput(?string $label = 'Submit', array $attributes = []): Input
{
    $tag = Input::submitButton($label);
    if (!empty($attributes)) {
        $tag = $tag->addAttributes($attributes);
    }
    return $tag;
}

            
table() public static method

Generates a {@see Table} tag.

public static Yiisoft\Html\Tag\Table table ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function table(array $attributes = []): Table
{
    $tag = new Table();
    return $attributes === [] ? $tag : $tag->attributes($attributes);
}

            
tag() public static method

Generates a complete HTML tag.

See also Yiisoft\Html\Tag\CustomTag.

public static Yiisoft\Html\Tag\CustomTag tag ( string $name, string|\Stringable|integer|float|null $content '', array $attributes = [] )
$name string

The tag name.

$content string|\Stringable|integer|float|null

The tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function tag(string $name, string|Stringable|int|float|null $content = '', array $attributes = []): CustomTag
{
    $tag = new CustomTag($name);
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
tbody() public static method

Generates a {@see Tbody} tag.

public static Yiisoft\Html\Tag\Tbody tbody ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function tbody(array $attributes = []): Tbody
{
    $tag = new Tbody();
    return $attributes === [] ? $tag : $tag->attributes($attributes);
}

            
td() public static method

Generates a {@see Td} tag.

public static Yiisoft\Html\Tag\Td td ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function td(string|Stringable|int|float|null $content = '', array $attributes = []): Td
{
    $tag = new Td();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if ($attributes !== []) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
textInput() public static method

Generates a text {@see Input} field.

public static Yiisoft\Html\Tag\Input textInput ( string|null $name null, boolean|float|integer|string|\Stringable|null $value null, array $attributes = [] )
$name string|null

The name attribute.

$value boolean|float|integer|string|\Stringable|null

The value attribute.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function textInput(
    ?string $name = null,
    bool|float|int|string|Stringable|null $value = null,
    array $attributes = [],
): Input {
    $tag = Input::text($name, $value);
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
textarea() public static method

Generates a {@see Textarea} input.

public static Yiisoft\Html\Tag\Textarea textarea ( string|null $name null, string|string[]|\Stringable|null $value null, array $attributes = [] )
$name string|null

The input name.

$value string|string[]|\Stringable|null

The input value.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function textarea(?string $name = null, string|Stringable|array|null $value = null, array $attributes = []): Textarea
{
    $tag = new Textarea();
    if ($name !== null) {
        $tag = $tag->name($name);
    }
    if (!empty($value)) {
        $tag = $tag->value($value);
    }
    return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}

            
tfoot() public static method

Generates a {@see Tfoot} tag.

public static Yiisoft\Html\Tag\Tfoot tfoot ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function tfoot(array $attributes = []): Tfoot
{
    $tag = new Tfoot();
    return $attributes === [] ? $tag : $tag->attributes($attributes);
}

            
th() public static method

Generates a {@see Th} tag.

public static Yiisoft\Html\Tag\Th th ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function th(string|Stringable|int|float|null $content = '', array $attributes = []): Th
{
    $tag = new Th();
    if ($content !== '') {
        $tag = $tag->content($content);
    }
    if ($attributes !== []) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
thead() public static method

Generates a {@see Thead} tag.

public static Yiisoft\Html\Tag\Thead thead ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function thead(array $attributes = []): Thead
{
    $tag = new Thead();
    return $attributes === [] ? $tag : $tag->attributes($attributes);
}

            
title() public static method

Generates a {@see Title} tag.

public static Yiisoft\Html\Tag\Title title ( string|\Stringable|integer|float|null $content '', array $attributes = [] )
$content string|\Stringable|integer|float|null

Tag content.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function title(string|Stringable|int|float|null $content = '', array $attributes = []): Title
{
    $tag = new Title();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $content === '' ? $tag : $tag->content($content);
}

            
tr() public static method

Generates a {@see Tr} tag.

public static Yiisoft\Html\Tag\Tr tr ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function tr(array $attributes = []): Tr
{
    $tag = new Tr();
    return $attributes === [] ? $tag : $tag->attributes($attributes);
}

            
track() public static method

Generates a {@see Track} tag.

public static Yiisoft\Html\Tag\Track track ( ?string $src null )
$src ?string

                public static function track(?string $src = null): Track
{
    $tag = new Track();
    return $src === null ? $tag : $tag->src($src);
}

            
ul() public static method

Generates a {@see Ul} tag.

public static Yiisoft\Html\Tag\Ul ul ( array $attributes = [] )
$attributes array

The tag attributes in terms of name-value pairs.

                public static function ul(array $attributes = []): Ul
{
    $tag = new Ul();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}

            
video() public static method

Generates a {@see Video} tag.

public static Yiisoft\Html\Tag\Video video ( )

                public static function video(): Video
{
    return new Video();
}

            
voidTag() public static method

Generates a void HTML tag.

See also Yiisoft\Html\Tag\CustomTag.

public static Yiisoft\Html\Tag\CustomTag voidTag ( string $name, array $attributes = [] )
$name string

The tag name.

$attributes array

The tag attributes in terms of name-value pairs.

                public static function voidTag(string $name, array $attributes = []): CustomTag
{
    $tag = (new CustomTag($name))->void();
    if (!empty($attributes)) {
        $tag = $tag->attributes($attributes);
    }
    return $tag;
}