而不是像这样对我的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":{}}
最好将模板包含在模板中,这样可以将模板渲染保留在视图层中,将逻辑保留在控制器层中。
但是,如果您真的想要...
您可以使用2种服务来做到这一点:twig
使用Twig_Environment
或templating.engine.twig
在Symfony2中为Twig构建的模板层,可以很容易地切换ot templating.engine.php
。
如果您使用该twig
服务,则可以查看有关如何使用它的树枝文档:
$template = $this->get('twig')->render('/full/path/to/Resources/views/'.$content[$i]['template'].'/view.html.twig', array(...));
如果您使用该templating.engine.twig
服务,请参阅有关如何使用它的模板文档,该文档与几乎完全相同Twig_Environment
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句