Final Class Yiisoft\Html\Html
| Inheritance | Yiisoft\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 $options parameter. See the
documentation of the {@see \Yiisoft\Html\tag()} method for more details.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| a() | Generates a hyperlink tag. | Yiisoft\Html\Html |
| addCssClass() | Adds a CSS class (or several classes) to the specified options. | Yiisoft\Html\Html |
| addCssStyle() | Adds the specified CSS styles to the HTML options. | 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() | Returns an autogenerated sequential ID. | 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 |
| 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 |
| nav() | Generates a {@see Nav} 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 options. | Yiisoft\Html\Html |
| removeCssStyle() | Removes the specified CSS styles from the HTML options. | 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
| 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 |
| ATTRIBUTE_ORDER | [ 'type', 'id', 'class', 'name', 'value', 'href', 'loading', 'src', 'srcset', 'form', 'action', 'method', 'selected', 'checked', 'readonly', 'disabled', 'multiple', 'size', 'maxlength', 'minlength', 'width', 'height', 'rows', 'cols', 'alt', 'title', 'rel', 'media', ] | The preferred order of attributes in a tag. This mainly affects the order of the attributes that are rendered by {@see renderTagAttributes()}. | 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
Generates a hyperlink tag.
See also Yiisoft\Html\Tag\A.
| public static Yiisoft\Html\Tag\A a ( string|\Stringable $content = '', string|null $url = null, array $attributes = [] ) | ||
| $content | string|\Stringable |
The tag content. |
| $url | string|null | |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function a(string|Stringable $content = '', ?string $url = null, array $attributes = []): A
{
$tag = A::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
if ($content !== '') {
$tag = $tag->content($content);
}
if ($url !== null) {
$tag = $tag->url($url);
}
return $tag;
}
Adds a CSS class (or several classes) to the specified options.
If the CSS class is already in the options, it will not be added again. If class specification at given options is an array, and some class placed there with the named (string) key, overriding of such key will have no effect. For example:
$options = ['class' => ['persistent' => 'initial']];
// ['class' => ['persistent' => 'initial']];
Html::addCssClass($options, ['persistent' => 'override']);
See also removeCssClass().
| public static void addCssClass ( array &$options, \BackedEnum|\BackedEnum[]|null[]|string|string[]|null $class ) | ||
| $options | array |
The options 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 &$options, 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($options['class'])) {
if (is_array($options['class'])) {
/** @psalm-var string[] $options['class'] */
$options['class'] = self::mergeCssClasses($options['class'], (array) $class);
} else {
/**
* @psalm-var string $options['class']
* @var string[] $classes We assume that `$options['class']` is valid UTF-8 string, so `preg_split()`
* never returns `false`.
*/
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY);
$classes = self::mergeCssClasses($classes, (array) $class);
$options['class'] = is_array($class) ? $classes : implode(' ', $classes);
}
} else {
$options['class'] = $class;
}
}
Adds the specified CSS styles to the HTML options.
If the options 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($options, 'width: 100px; height: 200px');
See also removeCssStyle().
| public static void addCssStyle ( array &$options, string|string[] $style, boolean $overwrite = true ) | ||
| $options | array |
The HTML options to be modified. |
| $style | string|string[] |
The new style string (e.g. |
| $overwrite | boolean |
Whether to overwrite existing CSS properties if the new style contain them too. |
public static function addCssStyle(array &$options, array|string $style, bool $overwrite = true): void
{
if (!empty($options['style'])) {
/** @psalm-var array<string,string>|string $options['style'] */
$oldStyle = is_array($options['style']) ? $options['style'] : self::cssStyleToArray($options['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);
}
$options['style'] = is_array($style) ? self::cssStyleFromArray($style) : $style;
}
Generates a {@see Address} tag.
| public static Yiisoft\Html\Tag\Address address ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function address(string|Stringable $content = '', array $attributes = []): Address
{
$tag = Address::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Article} tag.
| public static Yiisoft\Html\Tag\Article article ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function article(string|Stringable $content = '', array $attributes = []): Article
{
$tag = Article::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Aside} tag.
| public static Yiisoft\Html\Tag\Aside aside ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function aside(string|Stringable $content = '', array $attributes = []): Aside
{
$tag = Aside::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Audio} tag.
| public static Yiisoft\Html\Tag\Audio audio ( ) |
public static function audio(): Audio
{
return Audio::tag();
}
Generates a {@see B} tag.
| public static Yiisoft\Html\Tag\B b ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function b(string|Stringable $content = '', array $attributes = []): B
{
$tag = B::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Body} tag.
| public static Yiisoft\Html\Tag\Body body ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function body(string|Stringable $content = '', array $attributes = []): Body
{
$tag = Body::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Br} tag.
| public static Yiisoft\Html\Tag\Br br ( ) |
public static function br(): Br
{
return Br::tag();
}
Generates a {@see Caption} tag.
| public static Yiisoft\Html\Tag\Caption caption ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function caption(string|Stringable $content = '', array $attributes = []): Caption
{
$tag = Caption::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if ($attributes !== []) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
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);
}
Generates a list of checkboxes.
| public static Yiisoft\Html\Widget\CheckboxList\CheckboxList checkboxList ( string $name ) | ||
| $name | string | |
public static function checkboxList(string $name): CheckboxList
{
return CheckboxList::create($name);
}
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>";
}
Generates a {@see Code} tag.
| public static Yiisoft\Html\Tag\Code code ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. If content includes HTML tags or special characters like |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function code(string|Stringable $content = '', array $attributes = []): Code
{
$tag = Code::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
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 = Col::tag();
return $attributes === [] ? $tag : $tag->attributes($attributes);
}
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 = Colgroup::tag();
return $attributes === [] ? $tag : $tag->attributes($attributes);
}
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);
}
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;
}
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<string, string> $style ) | ||
| $style | array<string, string> |
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);
}
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;
}
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 = Datalist::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Generates a {@see Div} tag.
| public static Yiisoft\Html\Tag\Div div ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function div(string|Stringable $content = '', array $attributes = []): Div
{
$tag = Div::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Em} tag.
| public static Yiisoft\Html\Tag\Em em ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function em(string|Stringable $content = '', array $attributes = []): Em
{
$tag = Em::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
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,
);
}
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}" => '�', // U+0000 NULL
]);
}
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" => '	', // U+0009 CHARACTER TABULATION (tab)
"\n" => '
', // U+000A LINE FEED (LF)
"\u{000c}" => '', // U+000C FORM FEED (FF)
"\u{0000}" => '�', // U+0000 NULL
' ' => ' ', // U+0020 SPACE
'=' => '=', // U+003D EQUALS SIGN (=)
'`' => '`', // U+0060 GRAVE ACCENT (`)
]);
}
Escape special characters for use as JavaScript string value in a <script> tag:
<script></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, [
'/' => '\/',
'"' => '\"',
"'" => "\'",
'\\' => '\\\\',
]);
}
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 = Fieldset::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
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);
}
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 = Form::tag();
if ($action !== null) {
$attributes['action'] = $action;
}
if ($method !== null) {
$attributes['method'] = $method;
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Returns an autogenerated sequential ID.
| public static string generateId ( string $prefix = 'i' ) | ||
| $prefix | string | |
| return | string |
Autogenerated ID. |
|---|---|---|
public static function generateId(string $prefix = 'i'): string
{
$prefix .= hrtime(true);
if (isset(self::$generateIdCounter[$prefix])) {
$counter = ++self::$generateIdCounter[$prefix];
} else {
$counter = 1;
self::$generateIdCounter = [$prefix => $counter];
}
return $prefix . $counter;
}
| public static string getArrayableName ( string $name ) | ||
| $name | string | |
public static function getArrayableName(string $name): string
{
return !str_ends_with($name, '[]') ? $name . '[]' : $name;
}
| 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;
}
Generates a {@see H1} tag.
| public static Yiisoft\Html\Tag\H1 h1 ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function h1(string|Stringable $content = '', array $attributes = []): H1
{
$tag = H1::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see H2} tag.
| public static Yiisoft\Html\Tag\H2 h2 ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function h2(string|Stringable $content = '', array $attributes = []): H2
{
$tag = H2::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see H3} tag.
| public static Yiisoft\Html\Tag\H3 h3 ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function h3(string|Stringable $content = '', array $attributes = []): H3
{
$tag = H3::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see H4} tag.
| public static Yiisoft\Html\Tag\H4 h4 ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function h4(string|Stringable $content = '', array $attributes = []): H4
{
$tag = H4::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see H5} tag.
| public static Yiisoft\Html\Tag\H5 h5 ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function h5(string|Stringable $content = '', array $attributes = []): H5
{
$tag = H5::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see H6} tag.
| public static Yiisoft\Html\Tag\H6 h6 ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function h6(string|Stringable $content = '', array $attributes = []): H6
{
$tag = H6::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Header} tag.
| public static Yiisoft\Html\Tag\Header header ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function header(string|Stringable $content = '', array $attributes = []): Header
{
$tag = Header::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Hgroup} tag.
| public static Yiisoft\Html\Tag\Hgroup hgroup ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function hgroup(string|Stringable $content = '', array $attributes = []): Hgroup
{
$tag = Hgroup::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Hr} tag.
| public static Yiisoft\Html\Tag\Hr hr ( array $attributes = [] ) | ||
| $attributes | array | |
public static function hr(array $attributes = []): Hr
{
$tag = Hr::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Generates a {@see Html} tag.
| public static Yiisoft\Html\Tag\Html html ( string|\Stringable $content = '', string|null $lang = null, array $attributes = [] ) | ||
| $content | string|\Stringable |
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 $content = '', ?string $lang = null, array $attributes = []): Tag\Html
{
$tag = Tag\Html::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
if ($content !== '') {
$tag = $tag->content($content);
}
if ($lang !== null) {
$tag = $tag->lang($lang);
}
return $tag;
}
Generates a {@see I} tag.
| public static Yiisoft\Html\Tag\I i ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function i(string|Stringable $content = '', array $attributes = []): I
{
$tag = I::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
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 = Img::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
if ($url !== null) {
$tag = $tag->src($url);
}
if ($alt !== null) {
$tag = $tag->alt($alt);
}
return $tag;
}
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 |
| $value | boolean|float|integer|string|\Stringable|null |
The value attribute. If it is |
| $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 = Input::tag()->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;
}
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 = Script::tag()->url($url);
if (!empty($attributes)) {
$tag = $tag->addAttributes($attributes);
}
return $tag;
}
Generates a {@see Label} tag.
| public static Yiisoft\Html\Tag\Label label ( string|\Stringable $content = '', string|null $for = null ) | ||
| $content | string|\Stringable |
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 $content = '', ?string $for = null): Label
{
$tag = Label::tag();
if ($for !== null) {
$tag = $tag->forId($for);
}
if ($content !== '') {
$tag = $tag->content($content);
}
return $tag;
}
Generates a {@see Legend} tag.
| public static Yiisoft\Html\Tag\Legend legend ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
The tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function legend(string|Stringable $content = '', array $attributes = []): Legend
{
$tag = Legend::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Generates a {@see Li} tag.
| public static Yiisoft\Html\Tag\Li li ( string|\Stringable $content = '' ) | ||
| $content | string|\Stringable |
Tag content. |
public static function li(string|Stringable $content = ''): Li
{
$tag = Li::tag();
return $content === '' ? $tag : $tag->content($content);
}
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. |
public static function link(?string $url = null, array $attributes = []): Link
{
$tag = Link::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
if ($url !== null) {
$tag = $tag->url($url);
}
return $tag;
}
Generates a mailto hyperlink tag.
See also Yiisoft\Html\Tag\A.
| public static Yiisoft\Html\Tag\A mailto ( string $content, string|null $mail = null, array $attributes = [] ) | ||
| $content | string | |
| string|null | ||
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function mailto(string $content, ?string $mail = null, array $attributes = []): A
{
$tag = A::tag()
->content($content)
->mailto($mail ?? $content);
if (!empty($attributes)) {
$tag = $tag->addAttributes($attributes);
}
return $tag;
}
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 = Meta::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Generates a normal HTML tag.
See also Yiisoft\Html\Tag\CustomTag.
| public static Yiisoft\Html\Tag\CustomTag normalTag ( string $name, string|\Stringable $content = '', array $attributes = [] ) | ||
| $name | string |
The tag name. |
| $content | string|\Stringable |
The tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function normalTag(string $name, string|Stringable $content = '', array $attributes = []): CustomTag
{
$tag = CustomTag::name($name)->normal();
if ($content !== '') {
$tag = $tag->content($content);
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
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);
}
Generates a {@see Noscript} tag.
| public static Yiisoft\Html\Tag\Noscript noscript ( string|\Stringable $content = '' ) | ||
| $content | string|\Stringable |
Tag content. |
public static function noscript(string|Stringable $content = ''): Noscript
{
$tag = Noscript::tag();
return $content === '' ? $tag : $tag->content($content);
}
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 = Ol::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
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) . '>';
}
Generates a {@see Optgroup} tag.
| public static Yiisoft\Html\Tag\Optgroup optgroup ( ) |
public static function optgroup(): Optgroup
{
return Optgroup::tag();
}
Generates a {@see Option} tag.
| public static Yiisoft\Html\Tag\Option option ( string|\Stringable $content = '', boolean|float|integer|string|\Stringable|null $value = null ) | ||
| $content | string|\Stringable |
Tag content. |
| $value | boolean|float|integer|string|\Stringable|null |
The value attribute. |
public static function option(
string|Stringable $content = '',
bool|float|int|string|Stringable|null $value = null,
): Option {
$tag = Option::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if ($value !== null) {
$tag = $tag->value($value);
}
return $tag;
}
Generates a {@see P} tag.
| public static Yiisoft\Html\Tag\P p ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function p(string|Stringable $content = '', array $attributes = []): P
{
$tag = P::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
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);
}
Generates a {@see Picture} tag.
| public static Yiisoft\Html\Tag\Picture picture ( ) |
public static function picture(): Picture
{
return Picture::tag();
}
Generates a {@see Pre} tag.
| public static Yiisoft\Html\Tag\Pre pre ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. If content includes HTML tags or special characters like |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function pre(string|Stringable $content = '', array $attributes = []): Pre
{
$tag = Pre::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
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);
}
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 RadioList::create($name);
}
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);
}
Removes a CSS class from the specified options.
See also addCssClass().
| public static void removeCssClass ( array &$options, string|string[] $class ) | ||
| $options | array |
The options to be modified. |
| $class | string|string[] |
The CSS class(es) to be removed. |
public static function removeCssClass(array &$options, string|array $class): void
{
if (isset($options['class'])) {
if (is_array($options['class'])) {
$classes = array_diff($options['class'], (array) $class);
if (empty($classes)) {
unset($options['class']);
} else {
$options['class'] = $classes;
}
} else {
/** @var string[] */
$classes = preg_split('/\s+/', (string) $options['class'], -1, PREG_SPLIT_NO_EMPTY);
$classes = array_diff($classes, (array) $class);
if (empty($classes)) {
unset($options['class']);
} else {
$options['class'] = implode(' ', $classes);
}
}
}
}
Removes the specified CSS styles from the HTML options.
For example,
Html::removeCssStyle($options, ['width', 'height']);
See also addCssStyle().
| public static void removeCssStyle ( array &$options, string|string[] $properties ) | ||
| $options | array |
The HTML options 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 &$options, string|array $properties): void
{
if (!empty($options['style'])) {
/** @psalm-var array<string,string>|string $options['style'] */
$style = is_array($options['style']) ? $options['style'] : self::cssStyleToArray($options['style']);
foreach ((array) $properties as $property) {
unset($style[$property]);
}
$options['style'] = self::cssStyleFromArray($style);
}
}
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
{
if (count($attributes) > 1) {
$sorted = [];
foreach (self::ATTRIBUTE_ORDER as $name) {
if (isset($attributes[$name])) {
$sorted[$name] = $attributes[$name];
}
}
$attributes = array_merge($sorted, $attributes);
}
$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;
}
Generates a reset button tag.
See also Yiisoft\Html\Tag\Button::reset().
| public static Yiisoft\Html\Tag\Button resetButton ( string $content = 'Reset', array $attributes = [] ) | ||
| $content | string |
The content enclosed within the button tag. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function resetButton(string $content = 'Reset', array $attributes = []): Button
{
$tag = Button::reset($content);
if (!empty($attributes)) {
$tag = $tag->addAttributes($attributes);
}
return $tag;
}
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;
}
Generates a {@see Script} tag.
| public static Yiisoft\Html\Tag\Script script ( string $content = '', array $attributes = [] ) | ||
| $content | string |
The script content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function script(string $content = '', array $attributes = []): Script
{
$tag = Script::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Generates a {@see Section} tag.
| public static Yiisoft\Html\Tag\Section section ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function section(string|Stringable $content = '', array $attributes = []): Section
{
$tag = Section::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
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 = Select::tag();
if ($name !== null) {
$tag = $tag->name($name);
}
return $tag;
}
Generates a {@see Small} tag.
| public static Yiisoft\Html\Tag\Small small ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function small(string|Stringable $content = '', array $attributes = []): Small
{
$tag = Small::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Source} tag.
| public static Yiisoft\Html\Tag\Source source ( ) |
public static function source(): Source
{
return Source::tag();
}
Generates a {@see Span} tag.
| public static Yiisoft\Html\Tag\Span span ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function span(string|Stringable $content = '', array $attributes = []): Span
{
$tag = Span::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Strong} tag.
| public static Yiisoft\Html\Tag\Strong strong ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function strong(string|Stringable $content = '', array $attributes = []): Strong
{
$tag = Strong::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
Generates a {@see Style} tag.
| public static Yiisoft\Html\Tag\Style style ( string $content = '', array $attributes = [] ) | ||
| $content | string |
The style content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function style(string $content = '', array $attributes = []): Style
{
$tag = Style::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Generates a submit button tag.
See also Yiisoft\Html\Tag\Button::submit().
| public static Yiisoft\Html\Tag\Button submitButton ( string $content = 'Submit', array $attributes = [] ) | ||
| $content | string |
The content enclosed within the button tag. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function submitButton(string $content = 'Submit', array $attributes = []): Button
{
$tag = Button::submit($content);
if (!empty($attributes)) {
$tag = $tag->addAttributes($attributes);
}
return $tag;
}
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;
}
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 = Table::tag();
return $attributes === [] ? $tag : $tag->attributes($attributes);
}
Generates a complete HTML tag.
See also Yiisoft\Html\Tag\CustomTag.
| public static Yiisoft\Html\Tag\CustomTag tag ( string $name, string|\Stringable $content = '', array $attributes = [] ) | ||
| $name | string |
The tag name. |
| $content | string|\Stringable |
The tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function tag(string $name, string|Stringable $content = '', array $attributes = []): CustomTag
{
$tag = CustomTag::name($name);
if ($content !== '') {
$tag = $tag->content($content);
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
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 = Tbody::tag();
return $attributes === [] ? $tag : $tag->attributes($attributes);
}
Generates a {@see Td} tag.
| public static Yiisoft\Html\Tag\Td td ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function td(string|Stringable $content = '', array $attributes = []): Td
{
$tag = Td::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if ($attributes !== []) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
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);
}
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 = Textarea::tag();
if ($name !== null) {
$tag = $tag->name($name);
}
if (!empty($value)) {
$tag = $tag->value($value);
}
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
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 = Tfoot::tag();
return $attributes === [] ? $tag : $tag->attributes($attributes);
}
Generates a {@see Th} tag.
| public static Yiisoft\Html\Tag\Th th ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function th(string|Stringable $content = '', array $attributes = []): Th
{
$tag = Th::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if ($attributes !== []) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
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 = Thead::tag();
return $attributes === [] ? $tag : $tag->attributes($attributes);
}
Generates a {@see Title} tag.
| public static Yiisoft\Html\Tag\Title title ( string|\Stringable $content = '', array $attributes = [] ) | ||
| $content | string|\Stringable |
Tag content. |
| $attributes | array |
The tag attributes in terms of name-value pairs. |
public static function title(string|Stringable $content = '', array $attributes = []): Title
{
$tag = Title::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $content === '' ? $tag : $tag->content($content);
}
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 = Tr::tag();
return $attributes === [] ? $tag : $tag->attributes($attributes);
}
Generates a {@see Track} tag.
| public static Yiisoft\Html\Tag\Track track ( string|null $src = null ) | ||
| $src | string|null | |
public static function track(?string $src = null): Track
{
$tag = Track::tag();
return $src === null ? $tag : $tag->src($src);
}
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 = Ul::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Generates a {@see Video} tag.
| public static Yiisoft\Html\Tag\Video video ( ) |
public static function video(): Video
{
return Video::tag();
}
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 = CustomTag::name($name)->void();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}
Signup or Login in order to comment.