包含具有多个参数的 Twig [英] Include Twig with multiple parameters

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

问题描述

有没有一种方法可以包含一个具有多个参数的 Twig 模板?

我试过了,但没有用:

以下 Twig 由我的 Symfony 控制器渲染:

{% for object in objects %}{% if object.type == "simple" %}{% 包含 'BBLWebBundle:content:simple.html.twig'with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %}{% elseif object.type == "mp3" %}{% 包含 'BBLWebBundle:content:mp3.html.twig'with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %}{% elseif object.type == "video" %}{% 包含 'BBLWebBundle:content:video.html.twig'with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %}{% 万一 %}{% 结束为 %}

Controller 还传递一些参数(这只是一些 Dummy-Data):

 $objects['ob1']['type'] = "simple";$objects['ob1']['picture'] = "这是一张图片";$objects['ob1']['link'] = "#";$objects['ob1']['info'] = "哦,哇,一些信息";$objects['ob1']['name'] = "Potato";return $this->render('BBLWebBundle:Base:content.html.twig',array('objects' => $objects, 'title' => "我的标题很酷"));

这是一个应该包含的 Twig 模板:

{{图片}}

<div><a href="{{link }}"><h3>{{名称}}</h3></a><br/>{{ info }}<br/></div>

解决方案

这比你想象的要容易:

{% 包含 'BBLWebBundle:content:simple.html.twig'使用 {'picture': object.picture, 'link': object.link, 'name': object.name, 'info': 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} %}

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

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