Yii2 twig HTML帮助程序函数不工作

Yii2 twig HTML帮助程序函数不工作,twig,yii2,Twig,Yii2,问题 我正在使用Yi2和twig模板引擎。它似乎在工作,但我似乎不能使用Html助手的任何方法(yii/helpers/Html) 我的视图正在使用twig extends扩展基本布局{%extends“@layouts/_base.twig”%} 我将'yii/helpers/Html'包含在_base.twig文件中{{use('yii/helpers/Html')} 我正在使用{{html.encode(this.title)}}在页眉和 {{ html.submitButton('Sen

问题

我正在使用Yi2和twig模板引擎。它似乎在工作,但我似乎不能使用Html助手的任何方法(yii/helpers/Html)

我的视图正在使用twig extends扩展基本布局<代码>{%extends“@layouts/_base.twig”%}

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

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

{{ html.submitButton('Send Message', {
   'class': 'button button--cta button--expand',
}) | raw }}
尝试在我的视图中渲染一个按钮,但这两个按钮似乎都不起作用,我没有得到任何错误,只是渲染失败

问题

我的设置正确吗?在yii2 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 %}
{{ 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() }}
{%extends“@layouts/_base.twig”%}
{%块布局%}
...
{%set form=active\u form\u begin({
“id”:“联系我们表单”
}) %}
...
{{form.field(联系我们,'全名').textArea([{
“行”:6,
“占位符”:“如果您有任何问题,请告诉我们…”
}]).label('消息'{
'class':'label--inline'
})|生的}
{{html.submitButton('发送消息'{
“类”:“按钮按钮--cta按钮--展开”,
})|生的}
{{active_form_end()}}
{%endblock%}
\u基部树枝

{% 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 %}
{{ 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() }}
{{use('yii/helpers/Html')}
{{use('yii/widgets/ActiveForm')}
{{use('yii/web/jqueryaset')}
{{this.beginPage()}}
{{this.head()}}
{{html.csrfMetaTags()| raw}
{{register_asset_bundle('www/assets/AppAsset')}
{{html.encode(this.title)}
{{this.beginBody()}}
{%块头%}
{%include“@layouts/components/header.twig”%}
{%endblock%}
{%块布局%}
{%endblock%}
{%block footer%}
{%include“@layouts/components/footer.twig”%}
{%endblock%}
{{this.endBody()}}
{{this.endPage()}}

要在细枝模板中使用\yii/helpers\Html helper,您需要相应地配置细枝扩展。这将在“附加配置”页面中介绍

您需要在配置的“细枝”部分中添加“全局”选项(请参阅“此行”标记):

对于yiisoft/yii2细枝版本2.1.0:

[
“组件”=>[
“查看”=>[
'class'=>'yii\web\View',
“渲染器”=>[
“细枝”=>[
'class'=>'yii\twig\ViewRenderer',
'cachePath'=>'@runtime/Twig/cache',
//细枝选项数组:
“选项”=>[
“自动重新加载”=>真,
],
/**此行**/'globals'=>['html'=>['class'=>'\yii\helpers\html']],
'使用'=>['yii\bootstrap'],
],
// ...
],
],
],
]
然后在模板中将其用作“html”变量,例如:

{{ html.csrfMetaTags() | raw }}
对于yiisoft/yii2 twig版本2.0.6和更早版本有旧语法:

'globals' => ['html' => '\yii\helpers\Html'], // *THIS LINE*
根据实际情况,正确的形式必须是:

    [
    '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”在我最近的一个项目中不起作用。

出于某种原因,它不再起作用了@hesselek的答案确实有效。@谢谢,看起来yii2 twig扩展的v2.1.0中的语法已经更改。我更新了我的答案。