包含具有多个参数的 Twig

Posted

技术标签:

【中文标题】包含具有多个参数的 Twig【英文标题】:Include Twig with multiple parameters 【发布时间】:2013-04-07 20:17:12 【问题描述】:

有没有办法让 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 %    

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>

【问题讨论】:

【参考方案1】:

这比你想象的要容易:

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

【讨论】:

感谢您的快速响应,真的比我想象的要容易。 如果object.pictureobject.link 为空怎么办?如果它为空,我如何传递``值?【参考方案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。它将使用从列表中找到的第一个。 More information can be found here

【讨论】:

以上是关于包含具有多个参数的 Twig的主要内容,如果未能解决你的问题,请参考以下文章

具有多个参数的 JPA 标准 API

Django - 具有多个参数的过滤器

具有多个参数的 Excel 到 SQL 存储过程

SQL - 创建具有多个可选参数的存储过程

Symfony中的多个地址的一种形式 - Twig

如何并行化具有多个参数的简单 python def [重复]