php – 如何将TWIG输出渲染为变量供以后使用(symfony2)?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何将TWIG输出渲染为变量供以后使用(symfony2)?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
而不是这样渲染我的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":{}}
如果您使用 include the template in the template更好,这样您就可以在视图层和控制器层中保留模板渲染.

但是,如果你真的想要它…

您可以使用2个服务来实现:twig使用Twig_environment或templating.engine.twig,它是Symfony2 for Twig中的模板层,这可以轻松地切换为templateating.engine.PHP.

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

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何将TWIG输出渲染为变量供以后使用(symfony2)?全部内容,希望文章能够帮你解决php – 如何将TWIG输出渲染为变量供以后使用(symfony2)?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。