2012-01-06 155 views
7

我想主題我的形式,使得該領域的標籤顯示當前語言環境,像如何將變量傳遞給form_theme?

名稱(中文):

所以我想改寫塊generic_label這樣的:

{# form_theme.html.twig #} 

{% block generic_label %} 
{% spaceless %} 
    {% if required %} 
     {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %} 
    {% endif %} 
    <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }} (app.session.locale)</label> 
{% endspaceless %} 
{% endblock %} 

並導入它在我的模板:

{% form_theme options 'myBundle:Object:form_theme.html.twig' %} 

但應用程序變量在表單模板中不可訪問。 如何將變量傳遞給表單主題?

回答

0

如果app變量在表單主題中不可用,則可能是一個錯誤。我建議你create a ticket

在此期間,您可以使用當前模板作爲主題。喜歡的東西...

{% form_theme form _self %} 

{% block field_label %} 
    {% set attr = attr|merge({ 'for': id }) %} 
    {% if required %} 
     {% set attr = attr|merge({ 'class': attr.class|default('') ~ ' required' }) %} 
    {% endif %} 
    <label{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans }} ({{ app.session.locale }})</label> 
{% endblock %} 

如果您正在使用的Symfony主(2.1)與app.request.locale取代app.session.locale

+0

也不管用,'{%form_theme edit_form _self%} {%block field_label%} {{app.session.locale}} {%endblock%}'給出'變量「app」不存在'。我要創建一張票 – Matthieu 2012-01-08 10:59:17

+0

https://github.com/symfony/symfony/issues/3058 – Matthieu 2012-01-08 11:14:29

3

在當前版本的樹枝中(與2016年一樣),這是可能的。 在模板中使用下面這個樣子:

{{ form_row(form.content, {'testvar' : 'this is test variable'}) }} 

然後,在你的主題文件,只需使用:

{{testvar}} 

當然不是form.content你會使用你所需要的字段名。 乾杯, Chris