【发布时间】:2018-01-17 22:52:39
【问题描述】:
我正在尝试渲染一个没有布局、只有几个变量和一些循环的 Twig 模板。
当我在 Twig 上调用 render() 函数时,它会为以下类输出一段 PHP 代码:
php
/** path/to/my/template.html.twig */
class __TwigTemplate_435244378aba3a3f94258b7d2af4d53eb7a41acb741dd3ad0efcac038b621c67 extends Twig_Template
{
// bunch of methods for Twig_Template,
// including the compiled version of my template
}
在此之后,它给了我一个堆栈跟踪,但有以下异常:
未能加载 Twig 模板“path/to/my/template.html.twig”,索引“”:“path/to/my/template.html.twig”中的缓存已损坏。
目前我什至没有在这个应用程序中使用缓存,尽管添加缓存似乎没有什么不同。我正在使用这样的树枝:
// Factory to return a new Twig environment
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../views/');
return new \Twig_Environment($loader);
// My class has $this->twig set to the above object
$this->twig->render('path/to/my/template.html.twig', [
'report' => $report,
'file' => $file
]);
Twig 似乎能够读取我的模板,因为它在错误中输出的 PHP 代码块具有正确编译的模板版本。尝试设置可写的缓存目录仍然会导致相同的错误。
我在 Apache 下使用 Twig 1.34.4 和 PHP 5.6.29。
编辑
在某种程度上,有点成功。 Twig 似乎从不评估它生成的代码。
我编辑了 vendor/twig/twig/lib/Twig/Environment.php 文件并在第 448 行添加了以下内容:
eval("?><?" . $content);
之后我的模板渲染得很好。这让我得出结论,Twig 中的其他东西正在破坏,但这不是一个长期的解决方案,因为我不应该编辑供应商文件。
从第 456 行开始的块似乎表明 $content 应该有开头 <? 但我没有。所以这可能会影响编译。
是时候进行更多挖掘了。
【问题讨论】: