Templates 细枝在模板内渲染模板
我想知道如何呈现“另一个模板内的模板”。 我有这样的模板:Templates 细枝在模板内渲染模板,templates,twig,render,nodes,Templates,Twig,Render,Nodes,我想知道如何呈现“另一个模板内的模板”。 我有这样的模板: [...] {% autoescape false %} {{ page.content }} {% endautoescape %} [...] “page.content”可以用一些小树枝来估价! 例如,它可以包含一些调用,如 {{ myObject.poperty }} 甚至 {% render "MyBundle:Bundle:myAction" with {'title': myObject.title} %}
[...]
{% autoescape false %}
{{ page.content }}
{% endautoescape %}
[...]
“page.content”可以用一些小树枝来估价!
例如,它可以包含一些调用,如
{{ myObject.poperty }}
甚至
{% render "MyBundle:Bundle:myAction" with {'title': myObject.title} %}
默认情况下,page.content的内容不由细枝解析。
所以我做了一个扩展,可以解析如下调用
{{page.content}}
我的分机看起来像:
$loader = new \Twig_Loader_String();
$this->environment->setLoader($loader);
$template = $this->environment->loadTemplate($string);
$output = $template->display(array('myObject' => $object));
但是我仍然无法重新渲染第二种调用{%render…%}
我曾尝试:
$stream = $this->environment->tokenize($string);
$nodes = $this->environment->parse($stream)->getNode('body')->getNode(0);
$output = $this->environment->compile($nodes);
但是我被卡住了
您知道如何呈现我的“page.content”值吗?因此我创建了一个php细枝扩展,其中一个解决方案似乎是:
public function parsingTwig($source, $context)
{
$stream = $this->env->tokenize($source);
$iterator = $this->env->parse($stream)->getNode('body')->getIterator();
while ($iterator->valid())
{
$current = $iterator->current();
$output = $this->env->compile($current);
eval($output);
$iterator->next();
}
}
您需要定义一些函数,如“getAttribute”或“getContext”。这些函数可以在twi模板类中找到