Permalink
739815f Aug 22, 2017
1 contributor

Users who have contributed to this file

41 lines (31 sloc) 1.19 KB

Html helper

To use [[\yii\helpers\Html]] in templates you must add a Twig extension

'extensions' => [
    \yii\twig\html\HtmlHelperExtension::class,
],

and use it this way

{{ Html.a('text', 'link', {'class':'btn btn-default'}) }}

But you can't pass variables by reference for some \yii\helpers\Html methods. For example addCssClass() or addCssStyle(). Twig does not support this.

Use additional expression:

{% set favoriteButtonOptions = {
    'class': 'btn btn-default',
    'style': 'color:red'
} %}

{% css_class favoriteButtonOptions + 'btn-primary' %}
{# equivalent Html::addCssClass($favoriteButtonOptions, 'btn-primary') #}

{% css_class favoriteButtonOptions - 'btn-primary' %}
{# equivalent Html::removeCssClass($favoriteButtonOptions, 'btn-primary') #}

{% css_style favoriteButtonOptions + 'display:none' %}
{# equivalent Html::addCssStyle($favoriteButtonOptions, 'display:none') #}

{% css_style favoriteButtonOptions - 'display' %}
{# equivalent Html::removeCssStyle($favoriteButtonOptions, 'display') #}
или
{% css_style favoriteButtonOptions - ['display', 'color'] %}
{# equivalent Html::removeCssStyle($favoriteButtonOptions, ['display', 'color']) #}