symfony - 在 Twig 中动态调用宏?

标签 symfony twig

可以到 动态调用宏 在 Twig ?例如,这里有一个模板和一个名为“group”的宏,它使用 buttons 构建了一个按钮组。 array争论。还有另外两个宏,savedelete , 用于构建保存和删除按钮。

{# Make a group of buttons #}
{% macro group(buttons) %}
{% spaceless %}
    {% import "::macros.html.twig" as macros %}

    {% set content = '' %}

    {% for button in buttons %}
        {% set content = content ~ button %}
    {% endfor %}

    {{ macros.el('div', content, { 'class' : 'btn-group' }) }}
{% endspaceless %}
{% endmacro %}

{# Make a save button #}
{% macro save(attributes, size, image) %}
{% spaceless %}
    {{ _self.primary('save'|trans({}, 'buttons'), attributes, size, image) }}
{% endspaceless %}
{% endmacro %}

{# Make a delete button #}
{% macro delete(attributes, size, image) %}
{% spaceless %}
    {{ _self.danger('delete'|trans({}, 'buttons'), attributes, size, image) }}
{% endspaceless %}
{% endmacro %}

这很好地传递了 array按钮数:
{% import "::buttons.html.twig" as buttons %}
{% set items = [buttons.save, buttons.delete] %}
{{ buttons.group(items) }}

但我想传递宏名称 group宏:
{% import "::buttons.html.twig" as buttons %}
{{ buttons.group(['save', 'delete']) }}

并获得 savedelete自动调用的宏。这是可能的吗?

最佳答案

为什么不做

{% import "::buttons.html.twig" as buttons %}
{{ buttons.group([buttons.save, buttons.delete]) }}

关于symfony - 在 Twig 中动态调用宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556311/

相关文章:

forms - 使用 form_errors(form) plus 显示 symfony2 中每个字段的所有错误

php - 使用 Doctrine (Symfony2) 查询表时缺少行

symfony - 修复 Symfony2 中的要求

动态内容的 Symfony 翻译

Symfony2 FOSUserBundle 错误 - FileLoaderImportCircularReferenceException

php - 如何根据从 Symfony2 中的另一个列表中选择的值从一个选择列表中获取值?

Twig : auto_reload option in production recommended?

symfony - 如何在twig函数中使用绝对路径

php - Symfony/ Twig : How to change the money_pattern?

html - 根据父子评论显示在子评论下