包含具有多个参数的Twig

时间:2013-04-15 16:11:19

标签: arrays parameters include twig

有没有办法包含一个带有多个参数的Twig模板?

我尝试了这个,但它不起作用:

我的Symfony控制器呈现以下Twig:

{% 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>

2 个答案:

答案 0 :(得分:25)

比你想象的容易:

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

答案 1 :(得分:2)

现在已经四年了,现在你可以列出一个模板列表

所以你可以从

改变上面的代码
{% 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 object in objects %}
    {% include 'BBLWebBundle:content:' ~ object.type ~ '.html.twig' 
            with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %}
{% endfor %}

现在假设您没有每个object.type的模板,您只需将“默认”模板的路径添加到列表中,如:

{% 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 %}

因此,如果找不到object.type.html.twig,则会使用defualt.html.twig。它将使用从列表中找到的第一个。 More information can be found here