Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable into included twig template that has variable in template name

Tags:

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} ) %} 
like image 636
RHarrington Avatar asked Apr 12 '13 23:04

RHarrington


1 Answers

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'} %} 
like image 136
RHarrington Avatar answered Oct 19 '22 06:10

RHarrington