Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Twig variable in include path

Tags:

include

twig

I want to use a Twig variable in a include line, but it isn't working:
{% include 'folder/{{ component }}.twig' %}

I get the error: Unable to find template "folder/{{ component }}.twig"

Can anyone help me out?

Thanks!

like image 507
pascalvgemert Avatar asked Feb 10 '15 09:02

pascalvgemert


People also ask

How do you dump variables in Twig?

Using the Manage administrative menu, navigate to Configuration > Development > Devel settings (admin/config/development/devel) and under the heading, Variables Dumper, select Kint, then Save configuration. Like the dump function, kint() will not display any output unless debugging is enabled.

How do you set a variable in Twig template?

This can be accomplished by creating an array of the entry years. Inside the loop that's building the array, a conditional checks if the year already exists in the array... if the year exists, a variable is set to TRUE which you can use in your conditional later down on the page. Save this answer.

How do I add a global variable in Twig?

If you are using Twig in another project, you can set your globals directly in the environment: $twig = new Twig_Environment($loader); $twig->addGlobal('myStuff', $someVariable); And then use {{ myStuff }} anywhere in your application.

What is block in Twig?

Blocks are used for inheritance and act as placeholders and replacements at the same time. They are documented in detail in the documentation for the extends tag. Block names must consist of alphanumeric characters, and underscores. The first char can't be a digit and dashes are not permitted.


1 Answers

I just found the answer here: https://groups.google.com/forum/#!topic/twig-users/zfnxC16gHx0

The correct syntax is: {% include 'folder/' ~ component ~ '.twig' %}

like image 161
pascalvgemert Avatar answered Oct 10 '22 21:10

pascalvgemert