简体   繁体   中英

Twig extension seems to cause attempt to load non existent cache file

I have this twig extension for a silex based site:

<?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';
    }
}

Which is registered in the application file like this:

$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;
        }));

This seems correct - introducing mispelling in the extension class name causes a different exception to the one I'm stuck with.

I call this extension in my twig templates with {{ maybeDecodeJapanese() }} , which causes this exception (it doesn't seem to caused by the template call itself, mispelling it generates a standard function not found exception):

Class '__TwigTemplate_3318c38dfd9c3eb0c4193184a517ff94fdd975ffb45d7f0a8f7490718f3bc1ef' not found

This occurs in /silex/vendor/twig/twig/lib/Twig/Environment.php

My best guess is that this a cache file of some kind. Caching is disabled in the dev environment, but I've tried deleting the cache folder contents anyway, which hasn't helped. Googling has yet to provide any other leads.

Any help locating the source of the issue would be very helpful.

I think that there is error in function definition. Try to change it to:

...new \Twig_SimpleFunction("maybeDecodeJapanse", [$this, "maybeDecodeJapanse"]),...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM