Enable Twig with Multiple Options

Is there a way to enable a twig template with multiple options?

I tried this, but this did not work:

The following Twif controller is displayed by my Symfony controller:

{% for object in objects %} {% if object.type == "simple" %} {% include 'BBLWebBundle:content:simple.html.twig' with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% elseif object.type == "mp3" %} {% include 'BBLWebBundle:content:mp3.html.twig' with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% elseif object.type == "video" %} {% include 'BBLWebBundle:content:video.html.twig' with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% endif %} {% endfor %} 

The controller also passes some parameters (these are just a few Dummy-Data):

  $objects['ob1']['type'] = "simple"; $objects['ob1']['picture'] = "this is a picture"; $objects['ob1']['link'] = "#"; $objects['ob1']['info'] = "Oh wooow some Info"; $objects['ob1']['name'] = "Potato"; return $this->render('BBLWebBundle:Base:content.html.twig', array('objects' => $objects, 'title' => "Im very cool Title")); 

This is one Twig template that should be included:

 <div>{{ picture }}</div> <div><a href="{{ link }}"> <h3>{{ name }}</h3></a><br />{{ info }}<br /></div> 
+6
source share
2 answers

This is easier than you think:

 {% include 'BBLWebBundle:content:simple.html.twig' with {'picture': object.picture, 'link': object.link, 'name': object.name, 'info': object.info} %} 
+17
source

Now four years later, and now you can include a list of templates

So you can change the above code from

 {% for object in objects %} {% if object.type == "simple" %} {% include 'BBLWebBundle:content:simple.html.twig' with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% elseif object.type == "mp3" %} {% include 'BBLWebBundle:content:mp3.html.twig' with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% elseif object.type == "video" %} {% include 'BBLWebBundle:content:video.html.twig' with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% endif %} {% endfor %} 

for almost one liner made just as simple:

 {% for object in objects %} {% include 'BBLWebBundle:content:' ~ object.type ~ '.html.twig' with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% endfor %} 

Now imagine that you do not have a template for each object.type , all you have to do is add the path to the default template to the list, for example:

 {% for object in objects %} {% include [ 'BBLWebBundle:content:' ~ object.type ~ '.html.twig', 'BBLWebBundle:content:default.html.twig' ] with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %} {% endfor %} 

So if object.type.html.twig cannot be found, it will just use defualt.html.twig . He will use the first one found from the list. More detailed information can be found here

+1
source

All Articles