包括树枝多个参数 [英] Include Twig with multiple parameters

查看:134
本文介绍了包括树枝多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

下面的树枝被我的Symfony控制器呈现:

  {%为对象对象%}
        {%如果object.type ==简单%}
               {%包括BBLWebBundle:内容:simple.html.twig
                使用[{'图片':object.picture},{链接:object.link},{'名':object.name},{'信息':object.info}]}%        {%ELSEIF object.type ==MP3%}
                {%包括BBLWebBundle:内容:mp3.html.twig
                使用[{'图片':object.picture},{链接:object.link},{'名':object.name},{'信息':object.info}]}%        {%ELSEIF object.type ==视频%}
                {%包括BBLWebBundle:内容:video.html.twig
                使用[{'图片':object.picture},{链接:object.link},{'名':object.name},{'信息':object.info}]}%
        {% 万一 %}
{%ENDFOR%}

该控制器还传递一些参数(这只是一些假数据):

  $对象['OB1'] ['类型'] =简单;
    $对象['OB1'] ['图片'] =这是一个图片;
    $对象['OB1'] ['链接'] =#;
    $对象['OB1'] ['信息'] =呵呵wooow一些信息;
    $对象['OB1'] ['名'] =土豆;
    返回$这个 - >渲染('BBLWebBundle:座机:content.html.twig',
            阵列('对象'=> $对象,'标题'=>中林非常酷的标题));

这是应该包含一根细枝模板:

 < D​​IV> {{图片}}< / DIV>
< D​​IV>< A HREF ={{链接}}> < H3> {{名}}< / H3>< / A>< BR /> {{资讯}}< BR />< / DIV>


解决方案

这是更容易比你想象的:

  {%包括BBLWebBundle:内容:simple.html.twig'
     与{'图片':object.picture,链接:object.link,'名':object.name,信息:object.info}%}

Is there a way to include a Twig-template with more than one parameter?

I tried this, but it didn't work:

The following Twig is rendered by my 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 %}    

The Controller also passes some parameters (this is just some 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"));

This is one Twig-template which should be included:

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

解决方案

It's easier than you think:

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

这篇关于包括树枝多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆