18

有没有办法包含一个具有多个参数的 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>
4

2 回答 2

40

这比你想象的要容易:

{% include 'BBLWebBundle:content:simple.html.twig' 
     with {'picture': object.picture, 'link': object.link, 'name': object.name, 'info': object.info} %}
于 2013-04-15T16:13:26.670 回答
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. 它将使用从列表中找到的第一个。更多信息可以在这里找到

于 2017-02-14T13:40:29.100 回答