包括树枝多个参数(Include Twig with multiple parameters)

2019-08-31 06:36发布

有没有一种方法,包括树枝模板与一个以上的参数?

我想这一点,但它没有工作:

下面的树枝被我的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 %}    

控制器也通过一些参数(这只是一些占位数据):

    $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"));

这是一个枝杈模板应该包括:

<div>{{ picture }}</div> 
<div><a href="{{ link }}"> <h3>{{ name }}</h3></a><br />{{ info }}<br /></div>

Answer 1:

它比你想象:

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


Answer 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 。 它将使用第一个会从列表中找到。 更多信息可以在这里找到



文章来源: Include Twig with multiple parameters