php 如何将 TWIG 输出渲染为变量供以后使用(symfony2)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20956660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render TWIG output to a variable for later use (symfony2)?
提问by Matt Welander
Instead of doing this rendering of each slide in my TWIG like this (see line 6):
而不是像这样对我的 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 %}
...Instead I would like to keep all of that logic in the controller, to just deliver to the view an array of ready slides. How can I do something like this (see line 9):
...相反,我想将所有这些逻辑保留在控制器中,只将一组准备好的幻灯片传递给视图。我该如何做这样的事情(见第 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,
));
}
All it gives me at the moment (the contents of $content[$i]['html']) is
目前它给我的一切($content[$i]['html'] 的内容)是
{"headers":{}}
采纳答案by Wouter J
It's better if you include the template in the template, this way you keep the templating rendering in the view layer and the logic in the controller layer.
如果在模板中包含模板会更好,这样您就可以将模板渲染保留在视图层中,并将逻辑保留在控制器层中。
But well, if you really want it...
但是,如果你真的想要它......
You can use 2 services to do that: twigis using the Twig_Environmentor templating.engine.twigwhich is a templating layer build in Symfony2 for Twig, this can be easily switched ot templating.engine.php.
您可以使用 2 个服务来做到这一点:twig使用Twig_Environment或templating.engine.twig是在 Symfony2 中为 Twig 构建的模板层,这可以轻松切换 ot templating.engine.php。
If you use the twigservice, you can see the twig docson how to use it:
如果您使用该twig服务,您可以查看有关如何使用它的 twig 文档:
$template = $this->get('twig')->render('/full/path/to/Resources/views/'.$content[$i]['template'].'/view.html.twig', array(...));
If you use the templating.engine.twigservice, see the templating docson how to use it, which is almost exact the same as the Twig_Environment.
如果您使用该templating.engine.twig服务,请参阅有关如何使用它的模板文档,这几乎与Twig_Environment.
回答by Liutas
You can get content form rendered object like this:
您可以像这样获取内容表单渲染对象:
$this->render('BizTVArchiveBundle:ContentTemplate:Some.html.twig', array())->getContent();
回答by joachim
Within a twig template, you can set the value of a printed variable like this:
在树枝模板中,您可以像这样设置打印变量的值:
{% set rendered %}
{{ var_to_print }}
{% endset %}
回答by Strnm
That is because $this->render()returns a Responseobject.
Instead of $this->render(), use $this->renderView().
那是因为$this->render()返回一个Response对象。而不是$this->render(),使用$this->renderView()。

