Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load templates with the statement {% use %} dynamically in twig symfony?

Tags:

twig

symfony

I'm doing a project in symfony and I would like to load a template with the use statement.

This is what I like to do:

{% for p in db.page. %}
    {% use 'PageBundle:Pages:' ~ p.template ~ '.html.twig' %}
{% endfor %}

But don't allow it. Returns the following error message:

The template references in a "use" statement must be a string

Can anyone help?
Thanks!

like image 780
Aitor Moreno Farré Avatar asked Nov 02 '25 13:11

Aitor Moreno Farré


1 Answers

Twig's use function cannot being called using dynamic variables or expressions like OP.

Twig Documentation says: Because use statements are resolved independently of the context passed tothe template, the template reference cannot be an expression.

Refer to this link for more info: Twig Issue

like image 174
NBPalomino Avatar answered Nov 05 '25 14:11

NBPalomino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!