有没有办法包含一个具有多个参数的 Twig 模板?
我试过这个,但没有奏效:
以下 Twig 由我的 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 %}
Controller 还传递一些参数(这只是一些 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>
最佳答案
这比你想象的要容易:
{% include 'BBLWebBundle:content:simple.html.twig'
with {'picture': object.picture, 'link': object.link, 'name': object.name, 'info': object.info} %}
关于arrays - 包含具有多个参数的 Twig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16019887/