Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig - Dynamic Template Include

Tags:

twig

I need to include templates with dynamic names:

I tried:

{% for plugin in plugins %}
    {% include 'plugins/{{ plugin.name }}/navbar_left.tpl' %}
{% endfor %}

But then it shows:

Fatal error: Uncaught exception 'Twig_Error_Loader' with message 'Unable to find template "plugins/{{ plugin.name }}/navbar_left.tpl"

As you can see it doesn't replace {{ plugin.name }}. How can I solve this?

like image 232
user1766080 Avatar asked Oct 22 '12 17:10

user1766080


1 Answers

It works like this:

{% include 'plugins/' ~ plugin.name ~ '/navbar_left.tpl' %}

As ~ concats strings in Twig.

like image 197
insertusernamehere Avatar answered Oct 21 '22 00:10

insertusernamehere