Reputation: 1049
Is it possible to pass variables into an included twig template, where the template name is a variable in itself?
{% include('MyMainBundle:MyEntity:' ~ entity.templateName) %}
works, but when I try to also pass a variable into this template, twig throws a syntax error.
{% include('MyMainBundle:MyEntity:' ~ entity.templateName, {'name' : myName} ) %}
Upvotes: 31
Views: 61826
Reputation: 1513
For a template name as a variable, I had to use this format:
{% include 'AcmeCalendarBundle:Default:cal_event_' ~ day.item.type ~ '.html.twig' with {'item': day.item} %}
Using
{{ include 'AcmeCalendarBundle:Default:cal_event_' ~ day.item.type ~ '.html.twig', {'item': day.item} }}
did not work.
Upvotes: 10
Reputation: 1049
I see what I was doing wrong. I had combined two different versions of include, one using {{ and the other using {% due to the symfony and twig docs showing different ways of including templates. This was as simple as removing the parenthesis from my initial code and inserting a with prior to defining the argument.
You can include a template like this per http://symfony.com/doc/current/book/templating.html#including-other-templates
{{ include('AcmeArticleBundle:Article:articleDetails.html.twig', {'article': article}) }}
Or like this per http://twig.sensiolabs.org/doc/tags/include.html
{% include 'template.html' with {'foo': 'bar'} %}
Upvotes: 64