繁体   English   中英

Twig模板尝试以无限循环呈现自身(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM