public static renderTagAttributes ( array $attributes ) : string | ||
$attributes | array | attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]]. |
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. |
public static function renderTagAttributes($attributes)
{
if (count($attributes) > 1) {
$sorted = [];
foreach (static::$attributeOrder as $name) {
if (isset($attributes[$name])) {
$sorted[$name] = $attributes[$name];
}
}
$attributes = array_merge($sorted, $attributes);
}
$html = '';
foreach ($attributes as $name => $value) {
if (is_bool($value)) {
if ($value) {
$html .= " {$name}";
}
} elseif (is_array($value)) {
if (in_array($name, static::$dataAttributes)) {
foreach ($value as $n => $v) {
if (is_array($v)) {
$html .= " {$name}-{$n}='" . Json::htmlEncode($v) . "'";
} else {
$html .= " {$name}-{$n}=\"" . static::encode($v) . '"';
}
}
} elseif ($name === 'class') {
if (empty($value)) {
continue;
}
$html .= " {$name}=\"" . static::encode(implode(' ', $value)) . '"';
} elseif ($name === 'style') {
if (empty($value)) {
continue;
}
$html .= " {$name}=\"" . static::encode(static::cssStyleFromArray($value)) . '"';
} else {
$html .= " {$name}='" . Json::htmlEncode($value) . "'";
}
} elseif ($value !== null) {
$html .= " {$name}=\"" . static::encode($value) . '"';
}
}
return $html;
}