0

我有一个基于 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;
        }));

这似乎是正确的——在扩展类名称中引入拼写错误会导致与我所坚持的不同的异常。

我在我的树枝模板中用 调用这个扩展{{ maybeDecodeJapanese() }},这会导致这个异常(它似乎不是由模板调用本身引起的,拼错它会生成一个标准函数未找到异常):

Class '__TwigTemplate_3318c38dfd9c3eb0c4193184a517ff94fdd975ffb45d7f0a8f7490718f3bc1ef' not found

这发生在 /silex/vendor/twig/twig/lib/Twig/Environment.php

我最好的猜测是这是某种缓存文件。在开发环境中禁用了缓存,但我已经尝试删除缓存文件夹内容,但没有帮助。谷歌搜索尚未提供任何其他线索。

任何找到问题根源的帮助都会非常有帮助。

4

1 回答 1

2

我认为函数定义存在错误。尝试将其更改为:

...new \Twig_SimpleFunction("maybeDecodeJapanse", [$this, "maybeDecodeJapanse"]),...
于 2016-04-28T15:15:31.567 回答