2014-03-30 21 views
1

例如
我在表單對象中有兩個成員。根據項目覆蓋表單呈現模板

form_widget(form.icon)

form_widget(form.name)

我已經改變了 'choice_widget_expanded'

{% block choice_widget_expanded %} 
{% spaceless %} 
    <table {{ block('widget_container_attributes') }}> 
    {% for child in form %} 
     <tr> 
     {{ form_widget(child) }} 
     {{ form_label(child) }} 
     </tr> 
    {% endfor %} 
    </table> 
{% endspaceless %} 
{% endblock choice_widget_expanded %} 

但是我想使它{{形式的影響。圖標}}只有

是否有可能?我怎麼能告訴傳遞給這個塊的對象是form.icon或form.name?

回答

1

要覆蓋標籤塊choice_widget_expanded您可以定義塊,並具有擴展性能用它喜歡在下面

{% block choice_widget_expanded %} 
{% spaceless %} 
    <table {{ block('widget_container_attributes') }}> 
    {% for child in form %} 
     <tr> 
     {{ form_widget(child) }} 
     {{ form_label_custom(child) }} 
     </tr> 
    {% endfor %} 
    </table> 
{% endspaceless %} 
{% endblock choice_widget_expanded %} 

而對於自定義標籤太form_label_custom

注意現在對於每一個選擇欄(並非全部爲 字段)您的新標籤將在行動中

{% block form_label_custom %} 
{% spaceless %} 
    {% if label is not sameas(false) %} 
     {% if not compound %} 
      {% set label_attr = label_attr|merge({'for': id}) %} /* you can skip this part for td */ 
     {% endif %} 
     {% if required %} 
      {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %} 
     {% endif %} 
     {% if label is empty %} 
      {% set label = name|humanize %} 
     {% endif %} 
     <td {% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</td> 
    {% endif %} 
{% endspaceless %} 
{% endblock form_label_custom %} 

,甚至更多,你可以定義定製form_widget_custom(child)塊覆蓋像

{% block form_widget_custom %} 
{% spaceless %} 
    {% if compound %} 
     {{ block('form_widget_compound') }} 
    {% else %} 
     {{ block('form_widget_simple') }} 
    {% endif %} 
{% endspaceless %} 
{% endblock form_widget_custom %} 

而現在渲染場

{{ form_widget_custom(form.icon) }} 
+1

感謝,它工作得很好! – whitebear