Fixes #13576: Added support of srcset to yii\helpers\Html::img()

This commit is contained in:
Nikolay Oleynikov
2017-02-18 02:23:20 +03:00
committed by Alexander Makarov
parent 8e1a5eccde
commit a26d28f8e3
3 changed files with 103 additions and 4 deletions

View File

@ -430,11 +430,22 @@ class BaseHtml
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
* @since 2.0.12 It is possible to pass the "srcset" option as an array which keys are
* descriptors and values are URLs. All URLs will be processed by [[Url::to()]].
* @return string the generated image tag
*/
public static function img($src, $options = [])
{
$options['src'] = Url::to($src);
if (isset($options['srcset']) && is_array($options['srcset'])) {
$srcset = [];
foreach ($options['srcset'] as $descriptor => $url) {
$srcset[] = Url::to($url) . ' ' . $descriptor;
}
$options['srcset'] = implode(',', $srcset);
}
if (!isset($options['alt'])) {
$options['alt'] = '';
}