是否可以在Twig中動態調用宏?例如,下面是一個名爲「group」的模板和宏,它使用buttons
array
參數構建按鈕組。還有兩個宏,save
和delete
,用於構建保存和刪除按鈕。動態調用Twig中的宏?
{# 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']) }}
,並得到save
和delete
自動調用宏。這是可能的和如何?