![](/img/trans.png)
[英]Render twig-template with dynamically placed HTML in Symfony2?
[英]Twig template trying to render itself in an infinite loop (Symfony2 app)
應在屏幕底部呈現的Web事件探查器欄引發“最大功能嵌套級別達到'100'”錯誤,因為它陷入了無限循環,試圖在生成的代碼中加載相同的模板文件。
這是引起無限循環的兩行代碼。
0.5359 7623912 96. __TwigTemplate_12d25e382d980637bca9380c409ddd59->__construct(???) /var/www/site/app/cache/dev/classes.php:7999
0.5359 7624296 97. Twig_Environment->loadTemplate(???, ???) /var/www/site/app/cache/dev/twig/12/d2/5e382d980637bca9380c409ddd59.php:10
我檢查了源代碼,似乎相同的Twig模板正在其構造函數中加載自身。 我在下面提供了這兩個文件的源代碼片段。 我確定我需要包括更多內容,但是我不確定要包含什么內容。
app / cache / dev / classes.php
public function loadTemplate($name, $index = null)
{
$cls = $this->getTemplateClass($name, $index);
if (isset($this->loadedTemplates[$cls])) {
return $this->loadedTemplates[$cls];
}
if (!class_exists($cls, false)) {
if (false === $cache = $this->getCacheFilename($name)) {
eval('?>'.$this->compileSource($this->loader->getSource($name), $name));
} else {
if (!is_file($cache) || ($this->isAutoReload() && !$this->isTemplateFresh($name, filemtime($cache)))) {
$this->writeCacheFile($cache, $this->compileSource($this->loader->getSource($name), $name));
}
require_once $cache;
}
}
if (!$this->runtimeInitialized) {
$this->initRuntime();
}
return $this->loadedTemplates[$cls] = new $cls($this); // CALLS THE CONSTRUCTOR BELOW
}
app / cache / dev / twig / 12 / d2 / 5e382d980637bca9380c409ddd59.php
public function __construct(Twig_Environment $env)
{
parent::__construct($env);
$this->parent = $this->env->loadTemplate("WebProfilerBundle:Profiler:layout.html.twig"); // CALLS THE loadTemplate METHOD ABOVE
$this->blocks = array(
'body' => array($this, 'block_body'),
'panel' => array($this, 'block_panel'),
);
}
我無法發表評論,因此我將其發布為解決方案。 抱歉。
有時,在服務器配置或htaccess中定義的某些重定向或重寫規則會導致無限的重寫或重定向。 我建議您修改路線,例如更改
/foo/bar/1
至
/foo/bar/1/
或者,嘗試檢查您的重定向或重寫規則。 我敢肯定,如果您100%確信自己的代碼是正確的,那么您會追上來的。
這很舊,但是最近我遇到了同樣的問題。
我嘗試清除緩存,提高了xdebug的限制,並檢查了樹枝文件中的循環包含,但這些都沒有幫助。
對我來說,解決方案是通過在我的require部分中添加以下代碼,以確保在composer.json
文件中加載了穩定版本的twig:
"twig/twig": "@stable"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.