有没有办法包含一个具有多个参数的 Twig 模板?
我试过这个,但没有奏效:
以下 Twig 由我的 Symfony 控制器渲染:
{% 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 %}
控制器还传递一些参数(这只是一些 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"));
这是一个应包含的 Twig 模板:
<div>{{ picture }}</div>
<div><a href="{{ link }}"> <h3>{{ name }}</h3></a><br />{{ info }}<br /></div>