我可以在循环中进行Twig导入吗?

时间:2015-06-23 19:37:31

标签: twig

有什么方法可以用循环在树枝上进行导入吗?

例如,我有一个代码,看起来如下all_widget_file_names是文件名数组

{% if 'dwtext' in all_widget_file_names %}
{% import "dnkn/widgets/dwtext.twig" as dwtext %}
{% endif %}

{% if 'dwpassword' in all_widget_file_names %}
{% import "dnkn/widgets/dwpassword.twig" as dwpassword %}
{% endif %}

{% if 'dwradio' in all_widget_file_names %}
{% import "dnkn/widgets/dwradio.twig" as dwradio %}
{% endif %}

{% if 'dwcheckbox' in all_widget_file_names %}
{% import "dnkn/widgets/dwcheckbox.twig" as dwcheckbox %}
{% endif %}

{% if 'dwmultiselect' in all_widget_file_names %}
{% import "dnkn/widgets/dwmultiselect.twig" as dwmultiselect %}
{% endif %}

{% if 'dwtextarea' in all_widget_file_names %}
{% import "dnkn/widgets/dwtextarea.twig" as dwtextarea %}
{% endif %}

{% if 'dwselect' in all_widget_file_names %}
{% import "dnkn/widgets/dwselect.twig" as dwselect %}
{% endif %}

{% if 'dwform' in all_widget_file_names %}
{% import "dnkn/widgets/dwform.twig" as dwform %}
{% endif %}

我可以做这样的事吗

{% for filename in all_widget_file_names %}

{% import "dnkn/widgets/"~filename ~".twig" as filename %}

{% endfor %}

我知道import语句不会在as之后获取变量的值吗?我还有其他办法让这段代码更干净吗?

0 个答案:

没有答案