arrays - 包含具有多个参数的 Twig

标签 arrays parameters include twig

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

相关文章:

java - 我无法理解以下代码

php - 使用数组值更新 MySQL 数据库

c++ - 类类型作为参数和作为属性

javascript - 有没有办法在原型(prototype)中使用函数参数作为 getter ?

filter - 谷歌分析 - 包括过滤器不起作用

php - bootstrap php 包含侧边栏

Ruby - 从数组中打印一个值

c# - 惊讶地发现 C# 编译器可以推断出信息较少的声明

function - 在我声明它们之后是否可以重新评估这里的字符串?

c++ - 在 lshkit 中构建 mplsh-run 的问题