yii\helpers\BaseHtml::renderTagAttributes PHP Méthode

renderTagAttributes() public static méthode

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 BaseHtml::encode. 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 renderTagAttributes ( array $attributes ) : string
$attributes array attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]].
Résultat 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;
    }