gpt4 book ai didi

php - Twig 内部结构 : setCompiler

转载 作者:行者123 更新时间:2023-12-05 07:55:59 25 4
gpt4 key购买 nike

我在从默认编译器正确更改为自定义编译器时遇到问题。看来我可能没有正确创建类或对象。

来自 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com