我目前正在使用独立的 twig(不使用 symphony 或 composer),并且在文档中没有找到关于如何在 php 中注册扩展的文档。
我的 index.php 文件看起来像这样
<?php
include 'exts/ext.php';
require_once 'Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader);
$twig->addExtension(new Test_Twig_Extension())
echo $twig->render('index.twig');
?>
我的扩展类看起来像这样(ext.php)
<?php
//require_once 'Twig/Extension.php';
//require_once 'Twig/ExtensionInterface.php';
class Test_Twig_Extension extends Twig_Extension {
public function getFunctions() {
return array(
new Twig_SimpleFunction('my_function', array($this,'my_function'))
);
}
public function my_function($arg1, $arg2) {
echo "Arg1: {$arg1} and Arg2: {$arg2}";
}
public function getName(){
return 'my_function';
}
}
?>
我收到以下错误:
致命错误:在第 12 行的 C:\xampp\htdocs\Twig\Extension.php 中找不到接口“Twig_ExtensionInterface”
我发现了很多用 yaml 设置它的文章,但我没有使用 yaml。
我确定我没有正确注册或没有正确设置。