我想在一个twig块中生成表头并在页面中重复使用它们,这个页面有大约5个不同的表,大致相同的标题.块代码是这样的:
{% block table_headers %}
    <th>Fiscal Year</th>
    <th>End Date</th>
    <th>Period Length</th>
    {% for item in result.FinancialStatements.COAMap.mapItem %}
        {% if item.statementType == statementType %}
            <th>{{ item._ }} ({{ item.coaItem }})</th>
        {% endif %}
    {% endfor %} 
{% endblock %}
上面代码中的关键字是
{% if item.statementType == statementType %}
我想传递statementType作为参数,我正在渲染块,如下所示:
{% render block.table_headers with {'statementType': 'INC'} %}
但这不起作用.我希望将块及其渲染保留在同一个文件中(但不同的块),以保持概念上的接近.
甚至可以使用这样的块吗?我查看了Symfony2文档,找不到任何暗示可以做到这一点的内容,但对我来说这似乎是一个明显的块使用方式.
Chr*_*ris 12
Symfony 2.2中的include标签有更新,可能会对此有所帮助.以下是新标记的示例:
{{ include('FTWGuildBundle:Help:popover.html.twig', {'content':helpContent,'title':helpTitle}) }}
这可能是您所需要的,因为它避免了对控制器执行子请求(render这样做)它会更好地执行.
在我的示例中,我将HTML包含在帮助弹出框中并提供标题和内容.
kxo*_*kxo 12
现在(Symfony的2,3 4+),我们可以使用与语法:
{% with {
            'myVar1': myValue1,
            'myVar2': myValue2
        }
%}
        {{ block('toolbar', myTemplate) }}
{% endwith %}
提交:https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
小智 7
{% render block.table_headers with {'statementType': 'INC'} %}Symfony不承认.你必须使用:
{% render "yourBundle:controleur:action" with { 'arg1' : 'value1', 'arg2' : 'value2' } %}
当使用block 函数时,子模板可以访问父变量:
{% set foo = 'bar' %}
{{ block('another_block') }}
在子模板中:
{% block another_block %}
    {{ foo }}
{% endblock %}
印刷:
bar
| 归档时间: | 
 | 
| 查看次数: | 42883 次 | 
| 最近记录: |