加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 14:04:38 所属栏目:PHP教程 来源:网络整理
导读:而不是这样渲染我的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
而不是这样渲染我的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几乎完全相同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读