2015-04-04 31 views
3

要處理我的表單集合,我有一個塊集合小部件的自定義表單主題。該塊集合小部件呈現爲一個表格,因此取決於block_collection_header和block_collection_body。symfony2 /小枝:如何使用包含在表單主題中使用的塊?

塊收集部件始終保持不變,但有時我自定義其他兩個塊,塊收集頭和塊收集身體

我的工作代碼:

{# From file myview.html.twig #} 
{% form_theme form ':Model:prototype_table_collection.html.twig' %} 
{% form(form) %} 

螞蟻這種形式的主題是以下內容:

{# From file ':Model:prototype_table_collection.html.twig' #} 
{% block collection_widget %} 
    {% spaceless %} 
     <div class="collection"> 
      {% if prototype is defined %} 
       {% set body = prototype %} 
       {% set attr = attr|merge({'data-prototype': block('collection_body') }) %} 
       {% set header = prototype %} 
       {% set attr = attr|merge({'data-header': block('collection_header') }) %} 
      {% endif %} 

      {% if form.vars.allow_delete is defined and form.vars.allow_delete %} 
       {% set allow_delete = true %} 
      {% else %} 
       {% set allow_delete = false %} 
      {% endif %} 


      <div {{ block('widget_container_attributes') }} class="protoype"> 
       {{ form_errors(form) }} 
       <table class="subtable table"> 
        <thead> 
         <tr class="headers" style="display: none;"> 
          {% if form.children|length > 0 %} 
           {% if form.children[0]|length > 0 %} 
            {% set header = form.children[0] %} 
            {{ block('collection_header') }} 
           {% endif %} 
          {% endif %} 
         </tr> 
        </thead> 
        <tbody class="container_rows"> 

        {% for rows in form %} 
        {% spaceless %} 
         {% if rows.children|length > 0 %} 
           {% set body = rows %} 
           {{ block('collection_body') }} 
         {% endif %} 
        {% endspaceless %} 
        {% endfor %} 

        </tbody> 
       </table> 
       {% if prototype is defined %} 
        {% if form.vars.attr['data-add_label'] is defined %} 
         {% set add_label = form.vars.attr['data-add_label'] ~ ' ' %} 
        {% else %} 
         {% set add_label = 'Ajouter ' %} 
        {% endif %} 
        <a href="#/" class="add_button btn btn-default btn-sm pull-right">{{ add_label }}<i class="fa fa-plus"></i></a> 
       {% endif %} 
       <br> 
      </div> 
     </div> 
    {% endspaceless %} 
{% endblock collection_widget %} 

{% block collection_header %} 
    {% for field in header %} 
     <th> 
      {% if 'checkbox' not in field.vars.block_prefixes %} 
       {{ form_label(field)|raw }} 
      {% else %} 
       {% if field.vars.attr['data-label'] is defined %} 
        {{ field.vars.attr['data-label'] }} 
       {% else %} 
        Options 
       {% endif %} 
      {% endif %} 
     </th> 
    {% endfor %} 
    {% if allow_delete %} 
     <th class="align_center">Supprimer</th> 
    {% endif %} 
{% endblock %} 

{% block collection_body %} 
    {% spaceless %} 
    {% set fieldNum = 1 %} 
    <tr class="row_to_delete child_collection"> 
     {{ form_errors(body) }} 
     {% for field in body %} 
      <td class="field{{ fieldNum }} data-label"> 
       {{ form_widget(field) }} 
       {{ form_errors(field) }} 
      </td> 
      {% set fieldNum = fieldNum + 1 %} 
     {% endfor %} 
     {% if allow_delete %} 
      <td class="align_center align_middle"><a href="#/" class="fmu_delete_button btn btn-default btn-xs"><i class="fa fa-times"></i></a></td> 
     {% endif %} 
    </tr> 
    {% endspaceless %} 
{% endblock %} 

我想使用的代碼和這是不工作:

的觀點保持不變

{# From file myview.html.twig #} 
{% form_theme form ':Model:prototype_table_collection.html.twig' %} 
{% form(form) %} 

在這裏,我試圖從第一塊

{# From file ':Model:prototype_table_collection.html.twig' #} 
{% block collection_widget %} 
    {{include(':Model:collection_widget.html.twig')}} 
{%end block%} 

{% block collection_header %} 
{#stays the same as the previous code for this block. It is called by the block collection_widget #} 
{%end block%} 

{% block collection_body %} 
{#stays the same as the previous code for this block. It is called by the block collection_widget #} 
{%end block%} 

新的外在文件內外部化的代碼:

{#From file ':Model:collection_widget.html.twig' #} 
{# Here I put the same exact code as I had before inside the block collection_widget, I'm not changing the code, I'm just trying to externalize this part #} 

包含不起作用,我的收藏不加載。

我嘗試過擴展布局,但它也不起作用。 實施例:

{# From file ':Model:prototype_table_collection.html.twig' #} 
{% extends :Model:parent.html.twig' %} 

{% block content %} 

{% block collection_header %} 
{#stays the same as the previous code for this block. It is called by the block collection_widget #} 
{%end block%} 

{% block collection_body %} 
{#stays the same as the previous code for this block. It is called by the block collection_widget #} 
{%end block%} 

{%end block%} 

和父:

{# From file ':Model:parent.html.twig' #} 

{% block collection_widget %} 
    {# same code as brefore #} 
{%end block%} 

{% block content %} 
{% endblock %} 

如何避免在我使用它的每一個表格模板重複該{%塊collection_widget%}碼?

+0

肯定,這是一個錯字,問題仍然是一樣的。我會添加他們的標記 – 2015-04-05 10:20:33

回答

5

我相信你正在尋找horizontal reuse功能:

水平重用是很少需要定期模板先進的嫩枝功能。它主要用於需要使模板塊可重用而不使用繼承的項目。

只包含主模板use標籤:

{# :Model:prototype_table_collection.html.twig #} 

{% use ':Model:collection_widget.html.twig' %} 

{% block collection_header %} 
    {# code #} 
{%end block%} 

{% block collection_body %} 
    {# code #} 
{%end block%} 

然後定義collection_widget塊,就好像它是prototype_table_collection.html.twig文件裏擺在首位:

{# :Model:collection_widget.html.twig #} 
{% block collection_widget %} 
    {# code #} 
{% endblock %} 
+1

,這實際上是工作,最後!非常感謝 :) – 2015-04-05 19:28:57

相關問題