- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在从默认编译器正确更改为自定义编译器时遇到问题。看来我可能没有正确创建类或对象。
来自 http://twig.sensiolabs.org/doc/internals.html“可以通过调用 setCompiler() 方法更改默认编译器 (Twig_Compiler):$twig->setCompiler($compiler);”
这是我尝试更改编译器的地方:
\Twig_Autoloader::register();
$loader = new \Twig_Loader_Filesystem($templateDirectory);
$twig = new \Twig_Environment($loader);
$compiler = new Config_Generator_Twig_Compiler($twig);
$twig->setCompiler($compiler);
但是我得到这个错误:
PHP Catchable fatal error: Argument 1 passed to Twig_Node_Module::compile() must be an instance of Twig_Compiler, instance of Config_Generator_Twig_Compiler given, called in /config_generator/Config_Generator_Twig_Compiler.php on line 87 and defined in /config_generator/lib/Twig/Node/Module.php on line 36
“Config_Generator_Twig_Compiler.php”只是 Twig 文件“Compiler.php”的一个副本,在顶部更改了几行:
<?php
require_once $rootDirectory.'/lib/Twig/CompilerInterface.php'; <---- added this line
class Config_Generator_Twig_Compiler implements Twig_CompilerInterface <---- changed from "class Twig_Compiler implements Twig_CompilerInterface"
此文件的其余部分当前与“Compiler.php”相同:
{...
public function compile(Twig_NodeInterface $node, $indentation = 0)
{
$this->lastLine = null;
$this->source = '';
$this->sourceOffset = 0;
// source code starts at 1 (as we then increment it when we encounter new lines)
$this->sourceLine = 1;
$this->indentation = $indentation;
if ($node instanceof Twig_Node_Module) {
$this->filename = $node->getAttribute('filename');
}
$node->compile($this); <---- here's line 87
return $this;
}
...}
最佳答案
$twig->setCompiler($object)
需要 $object
扩展或者是一个 Twig_Compiler
。
因此,您的类(class)必须扩展它。
class Config_Generator_Twig_Compiler extends Twig_Compiler
{
[... your code here ...]
}
Twig_Compiler
已经实现了 Twig_CompilerInterface
。
关于php - Twig 内部结构 : setCompiler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28996272/
我在从默认编译器正确更改为自定义编译器时遇到问题。看来我可能没有正确创建类或对象。 来自 http://twig.sensiolabs.org/doc/internals.html“可以通过调用 se
我是一名优秀的程序员,十分优秀!