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

2022-08-30 20:15:27

而不是像这样在我的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":{}}

答案 1

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

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

答案 2

在树枝模板中,您可以设置打印变量的值,如下所示:

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

推荐