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
source share