2012-03-04 56 views
4

是否可以在Twig中動態調用宏?例如,下面是一個名爲「group」的模板和宏,它使用buttonsarray參數構建按鈕組。還有兩個宏,savedelete,用於構建保存和刪除按鈕。動態調用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']) }} 

,並得到savedelete自動調用宏。這是可能的和如何?

回答

0

爲什麼不只是做

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