问题
Is there a way to include a Twig-template with more than one parameter?
I tried this, but it didn't work:
The following Twig is rendered 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 (this is just some 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 which should be included:
<div>{{ picture }}</div>
<div><a href="{{ link }}"> <h3>{{ name }}</h3></a><br />{{ info }}<br /></div>
回答1:
It's easier than you think:
{% include 'BBLWebBundle:content:simple.html.twig'
with {'picture': object.picture, 'link': object.link, 'name': object.name, 'info': object.info} %}
回答2:
Now it's four years later and now you can include a list of templates
So you could 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 %}
to an almost one liner doing exactly the same as simple as this:
{% 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 you don't have a template for every object.type all you have to do is add the path to a "default" template to the list like:
{% 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 like this if object.type.html.twig can not be found it will just use the defualt.html.twig. It will use the first one gets found from the list. More information can be found here
来源:https://stackoverflow.com/questions/16019887/include-twig-with-multiple-parameters