2015-11-04 84 views
1

問題Yii2樹枝HTML輔助功能不能正常工作

我使用yii2與樹枝模板引擎。它似乎工作,但我似乎無法使用任何的Html助手(yii/helpers/Html)的方法。

我的看法是擴展基礎佈局使用樹枝延伸。 {% extends "@layouts/_base.twig" %}

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

我使用{{ html.encode(this.title) }}呈現在標題的頁面標題和

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

,試圖在我看來呈現一個按鈕,但既不似乎工作,我沒有得到任何錯誤,只是沒有渲染。

問題

我有正確的設置呢?我需要做什麼才能在yii2樹枝中呈現按鈕?使用樹枝非常新穎。

代碼

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() }} 

回答

2

要使用\警予/傭工\ HTML幫助中的樹枝模板,你需要相應地配置枝條延伸。這包括在documentation,其他配置頁面中。

您需要添加配置的 '樹枝' 部分中 '全局' 選項(參見 'THIS LINE' 標記):

對於yiisoft/yii2-樹枝版本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-樹枝版本2.0.6一第二較早有舊的語法:

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

由於某種原因,這不再起作用。 @ hesselek的答案確實有效。 – clapas

+0

@clapas謝謝,看起來像yii2-twig擴展v2.1.0語法已經改變。我更新了我的答案。 – cronfy

1

根據實際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」=>'\警予\ helpers \ html'在我最近的一個項目中不起作用。