我对基于silex的网站有这个枝条扩展:
<?php
namespace UT\Provider;
/**
* Twig extension for containing any additional twig functions we need
*/
class TwigUTProvider extends \Twig_Extension {
public function getName() {
return 'ut_functions';
}
public function getFunctions() {
return [
new \Twig_SimpleFunction("maybeDecodeJapanse", [$this, \UT\Provider\TwigUTProvider::maybeDecodeJapanse()]),
];
}
public function maybeDecodeJapanse() {
return 'a string';
}
}
在应用程序文件中注册了这样的内容:
$this->register(new Provider\TwigServiceProvider, [
'twig.path' => [
__DIR__ . '/Resources/Templates',
$this['docroot'] . '/silex/vendor/braincrafted/bootstrap-bundle/Braincrafted/Bundle/BootstrapBundle/Resources/views/Form',
__DIR__ . '/../Floso/Templates',
],
'twig.options' => [
'auto_reload' => true,
'cache' => $this['debug_mode'] ? false : ($this['docroot'] . '/silex/var/cache/twig'),
'debug' => $this['debug_mode'],
],
]);
# Add Twig extensions
$this['twig'] = $this->share($this->extend('twig', function ($twig) {
$twig->addExtension(new \UT\Provider\TwigUTProvider());
$twig->addExtension(new BootstrapBundle\Twig\BootstrapIconExtension('glyphicon'));
$twig->addExtension(new BootstrapBundle\Twig\BootstrapLabelExtension);
$twig->addExtension(new BootstrapBundle\Twig\BootstrapBadgeExtension);
$twig->addExtension(new BootstrapBundle\Twig\BootstrapFormExtension);
$twig->addExtension(new \Twig_Extension_StringLoader());
$twig->getExtension('core')->setTimezone('Europe/London');
return $twig;
}));
这似乎是正确的 - 在扩展类名称中引入拼写错误会导致我遇到的异常异常。
我在我的twig模板中使用{{ maybeDecodeJapanese() }}
调用此扩展名,这会导致此异常(它似乎不是由模板调用本身引起的,错误拼写会生成标准函数未找到异常):
Class '__TwigTemplate_3318c38dfd9c3eb0c4193184a517ff94fdd975ffb45d7f0a8f7490718f3bc1ef' not found
这发生在/silex/vendor/twig/twig/lib/Twig/Environment.php
我最好的猜测是这是某种缓存文件。在开发环境中禁用了缓存,但我尝试删除缓存文件夹内容,但这没有帮助。谷歌搜索尚未提供任何其他线索。
找到问题根源的任何帮助都会非常有用。
答案 0 :(得分:2)
我认为功能定义中存在错误。尝试将其更改为:
...new \Twig_SimpleFunction("maybeDecodeJapanse", [$this, "maybeDecodeJapanse"]),...