Browse Source

Improve array support for "data" attribute in renderTagAttributes()

tags/2.0.0-rc
armab 10 years ago
parent
commit
3048fdf66f
  1. 8
      framework/helpers/BaseHtml.php

8
framework/helpers/BaseHtml.php

@ -1569,6 +1569,8 @@ class BaseHtml
* 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"`.
*
* @param array $attributes 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
@ -1595,7 +1597,11 @@ class BaseHtml
}
} elseif (is_array($value) && $name === 'data') {
foreach ($value as $n => $v) {
$html .= " $name-$n=\"" . static::encode($v) . '"';
if (is_array($v)) {
$html .= " $name-$n='" . Json::encode($v) . "'";
} else {
$html .= " $name-$n=\"" . static::encode($v) . '"';
}
}
} elseif ($value !== null) {
$html .= " $name=\"" . static::encode($value) . '"';

Loading…
Cancel
Save