user1766080
user1766080

Reputation: 631

Twig - Dynamic Template Include

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?

Upvotes: 22

Views: 13535

Answers (1)

insertusernamehere
insertusernamehere

Reputation: 23600

It works like this:

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

As ~ concats strings in Twig.

Upvotes: 46

Related Questions