【问题标题】:How to render TWIG output to a variable for later use (symfony2)?如何将 TWIG 输出渲染到变量以供以后使用(symfony2)?
【发布时间】:2014-01-24 06:02:54
【问题描述】:

不要像这样在我的 TWIG 中渲染每张幻灯片(参见第 6 行):

{# loop out the slides #}
{% for c in contents %}
    {% set i=i+1 %} {# increase slide number #}
    <div id="slide{{ i }}" class="slide" style="z-index:{{ i }};">
        {# the slide itself, rendered by it's own template #}
        {% include 'BizTVArchiveBundle:ContentTemplate:'~c.template~'/view.html.twig' with {'contents': c, 'ordernumber': i} %} 
    </div>
{% endfor %}

...相反,我想将所有这些逻辑保留在控制器中,只为视图提供一组准备好的幻灯片。我怎么能做这样的事情(见第 9 行):

    //do stuff...
    foreach ($container->getContent() as $c) {
                $content[$i]['title'] = $c->getTitle();
                $content[$i]['sort'] = $c->getSortOrder();
                $content[$i]['data'] = $c->getData();
                $content[$i]['template'] = $c->getTemplate()->getId();
                $content[$i]['duration'] = $this->extract_seconds($c); 

                $content[$i]['html'] = $this->render('BizTVArchiveBundle:ContentTemplate:'.$content[$i]['template'].'/view.html.twig', array(
                    'contents'=> $content[$i],
                    'ordernumber' => 99,
                ));
    }

目前它给我的只是($content[$i]['html'] 的内容)

{"headers":{}}

【问题讨论】:

    标签: php symfony twig render


    【解决方案1】:

    您可以像这样获取内容表单呈现的对象:

    $this->render('BizTVArchiveBundle:ContentTemplate:Some.html.twig', array())->getContent();
    

    【讨论】:

    • 不工作,它说Call to a member function getContent() on string
    【解决方案2】:

    在 twig 模板中,您可以像这样设置打印变量的值:

    {% set rendered %}
      {{ var_to_print }}
    {% endset %}
    

    【讨论】:

    • 当我尝试这个时我很惊讶。它也适用于旧的 1.xx 版本和 2.xx 版本。不确定 0.xx 版本,但我怀疑这些版本是否有效。
    • 哇,这是一颗真正的钻石!我找这个很久了。
    【解决方案3】:

    如果你include the template in the template更好,这样你就可以在视图层保持模板渲染,在控制器层保持逻辑。

    不过,如果你真的想要的话……

    您可以使用 2 个服务来做到这一点:twig 正在使用 Twig_Environmenttemplating.engine.twig 这是 Symfony2 中为 Twig 构建的模板层,这可以很容易地切换到 templating.engine.php

    如果您使用twig 服务,您可以查看the twig docs 了解如何使用它:

    $template = $this->get('twig')->render('/full/path/to/Resources/views/'.$content[$i]['template'].'/view.html.twig', array(...));
    

    如果您使用templating.engine.twig 服务,请参阅the templating docs 了解如何使用它,这与Twig_Environment 几乎完全相同。

    【讨论】:

    • 我如何知道要指定与什么相关的路径?当我开始输入路径时,就像“我在哪里”一样,我在 www 中吗?在我的应用程序文件夹中?我是否像往常一样在我的 sym2project/web 中(从 app.php 运行?)?
    • @MattiasSvensson 您可以使用__DIR__ 常量,这是使用该常量的 PHP 文件的目录。或者您可以使用模板解析器(不确定如何调用该服务)将已知的Bundle:Controller:view 格式解析为绝对路径
    • 我试过 DIR 。 '/../../ArchiveBundle/Resources/views/ 但它只是给我一个错误,它找不到模板。我知道它在那里。也许 ../ 这里不允许?我还尝试手动指定完整路径(基于 DIR 的格式),在我的例子中是 '/var/www/biztv/src/BizTV/ArchiveBundle/Resources/views/ContentTemplate/28/view.html.twig' 但是我得到同样的错误=(
    【解决方案4】:

    那是因为$this-&gt;render() 返回一个Response 对象。 代替$this-&gt;render(),使用$this-&gt;renderView()

    【讨论】:

    • 这似乎有效,尽管the docs中没有提到renderView
    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多