Yii2 twig HTML 辅助函数不工作

Yii2 twig HTML helper functions not working

问题

我将 yii2 与 twig 模板引擎一起使用。它似乎在工作,但我似乎无法使用 Html 助手 (yii/helpers/Html) 的任何方法。

我的观点是使用 twig extends 扩展基本布局。 {% extends "@layouts/_base.twig" %}

我将 'yii/helpers/Html' 包含在 _base.twig 文件中。 {{ use('yii/helpers/Html') }}

我正在使用 {{ html.encode(this.title) }} 在 header 和

中呈现页面标题
{{ html.submitButton('Send Message', {
   'class': 'button button--cta button--expand',
}) | raw }}

尝试在我的视图中呈现一个按钮,但似乎都不起作用,我没有收到任何错误,只是没有呈现。

问题

我设置正确了吗?我应该怎么做才能在 yii2 twig 中呈现一个按钮?对使用树枝非常陌生。

代码

index.twig

{% extends "@layouts/_base.twig" %}

{% block layout %}

...

  {% set form = active_form_begin({
    'id' : 'contact-us-form'
  }) %}

...

  <div class="row">
    <div class="medium-8 medium-offset-2 columns">

      {{ form.field(contact_us, 'full_name').textArea([{
        'rows' : 6,
        'placeholder' : 'Let us know if you have any questions...'
      }]).label('Message', {
        'class' : 'label--inline'
      }) | raw }}

      </div>
    </div>
    <div class="row">
      <div class="medium-3 medium-offset-2 columns">

        {{ html.submitButton('Send Message', {
          'class': 'button button--cta button--expand',
        }) | raw }}

      </div>
    </div>

    {{ active_form_end() }}
    </section>
{% endblock %}

_base.twig

{{ use('yii/helpers/Html') }}
{{ use('yii/widgets/ActiveForm') }}
{{ use('yii/web/JqueryAsset') }}

{{ this.beginPage() }}

<!DOCTYPE html>
<html lang="{{app.language}}">

    <head>
        {{ this.head() }}
        <meta charset="{{app.charset}}">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        <link href='https://fonts.googleapis.com/css?family=Lato:300,400,700,900' rel='stylesheet' type='text/css'>
        {{ html.csrfMetaTags() | raw }}
        {{ register_asset_bundle('www/assets/AppAsset') }}
        <title>{{ html.encode(this.title) }}</title>

    </head>

    <body>
    {{ this.beginBody() }}

    {% block header %}
        {% include "@layouts/components/header.twig" %}
    {% endblock %}

    {% block layout %}
    {% endblock %}

    {% block footer %}
        {% include "@layouts/components/footer.twig" %}
    {% endblock %}

    {{ this.endBody() }}
    </body>
</html>
{{ this.endPage() }}

要在 Twig 模板中使用 \yii/helpers\Html 助手,您需要相应地配置 Twig 扩展。这在 documentation,附加配置页面中介绍。

您需要在配置的 'twig' 部分添加 'globals' 选项(参见 'THIS LINE' 标记):

对于 yiisoft/yii2-twig 版本 2.1.0:

[
    'components' => [
        'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    // Array of twig options:
                    'options' => [
                        'auto_reload' => true,
                    ],
 /* *THIS LINE* */  'globals' => ['html' => ['class'=>'\yii\helpers\Html']],
                    'uses' => ['yii\bootstrap'],
                ],
                // ...
            ],
        ],
    ],
]

然后在模板中将其用作'html'变量,例如:

{{ html.csrfMetaTags() | raw }}

对于 yiisoft/yii2-twig 版本 2.0.6 和更早版本,有旧语法:

'globals' => ['html' => '\yii\helpers\Html'], // *THIS LINE*

根据实际documentation,正确的形式必须是:

    [
    'components' => [
        'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    // Array of twig options:
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'globals' => ['html' => ['class'=>'\yii\helpers\Html']], // *THIS LINE*
                    'uses' => ['yii\bootstrap'],
                ],
                // ...
            ],
        ],
    ],
]

我不知道为什么,以前的 "html"=>'\yii\helpers\html' 在我最近的一个项目中不起作用。