douyou1901 2015-04-04 23:27
浏览 95
已采纳

symfony2 / twig:如何在表单主题中使用的块中使用include?

To process my form collections I have a custom form theme for the block collection widget. This block collection widget is rendered as a table and so depends on a block_collection_header and a block_collection_body.

The block collection widget always stays the same, but sometimes I customize the two other blocks, block collection header and block collection body

My working code :

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

Ant this form theme is the following :

{# 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 %}

The code I'd like to use and which is not working:

The view stays the same

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

Here I am trying to externalize the code from within the first block

{# 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%}

The new externalized file :

{#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 #}

The include does not work, my collection does not load.

I have tried with extending a layout, it doesn't work either. Example :

{# 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%}

and the parent :

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

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

{% block content %}
{% endblock %}

How can I avoid repeating this {% block collection_widget %} code in every form template where I use it ?

  • 写回答

1条回答 默认 最新

  • douyong1974 2015-04-05 17:10
    关注

    I believe you are looking for the horizontal reuse functionality:

    Horizontal reuse is an advanced Twig feature that is hardly ever needed in regular templates. It is mainly used by projects that need to make template blocks reusable without using inheritance.

    Just include the use tag in the main template:

    {# :Model:prototype_table_collection.html.twig #}
    
    {% use ':Model:collection_widget.html.twig' %}
    
    {% block collection_header %}
        {# code #}
    {%end block%}
    
    {% block collection_body %}
        {# code #}
    {%end block%}
    

    Then define the collection_widget block as if it was inside of the prototype_table_collection.html.twig file in the first place:

    {# :Model:collection_widget.html.twig #}
    {% block collection_widget %}
        {# code #}
    {% endblock %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能