简体   繁体   English

Silex不会加载Twig扩展

[英]Silex won't load Twig extensions

I'm using Silex and trying to use the Intl extension for Twig, but I get the following error when trying to use the localizeddate filter: 我正在使用Silex并尝试为Twig使用Intl扩展名,但是在尝试使用localizeddate过滤器时出现以下错误:

The filter "localizeddate" does not exist in "games.html" 过滤器“ localizeddate”在“ games.html”中不存在

As per these instructions , my composer.json contains the following line in the require section: 按照这些说明 ,我的composer.jsonrequire部分包含以下行:

 "twig/extensions": "~1.1.0"

My app.php file contains this: 我的app.php文件包含以下内容:

$app->register(new TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/../templates/'
));

$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extensions_Extension_Intl($app));

    return $twig;
}));

Finally, somewhere in my games.html template is this, which throws the error described above: 最后,这是我的games.html模板中的某个地方,它引发了上述错误:

{{ game.start_time|localizeddate('medium', 'none', 'fr') }}

For the sake of testing, I also tried with the Text extension, exactly as in the linked instructions, but it generated the same kind of error. 为了进行测试,我也尝试使用Text扩展名,与链接说明中的完全相同,但是它会产生相同类型的错误。

The strange thing is I can put anything in the function where I add the extension; 奇怪的是,我可以在添加扩展名的函数中放入任何内容; it will never generate any error as long as the syntax is valid. 只要语法有效,它就不会产生任何错误。 So, if I replace $twig->addExtension(new Twig_Extensions_Extension_Intl($app)) by $twig->addExtension(new Foo()) , Silex doesn't seem to have a problem with it, even though the class Foo doesn't exist. 因此,如果我用$twig->addExtension(new Twig_Extensions_Extension_Intl($app)) $twig->addExtension(new Foo())代替$twig->addExtension(new Twig_Extensions_Extension_Intl($app)) ,即使类Foo没有,Silex似乎也没有问题不存在。

If I throw a RuntimeException in the constructor of the Twig_Extensions_Extension_Intl class, it doesn't show up either, so it seems the extension is never loaded in the first place. 如果我在Twig_Extensions_Extension_Intl类的构造函数中抛出RuntimeException ,则它也不会显示,因此似乎从来没有首先加载扩展。 This line can be found in the autoload_namespaces.php file though: 'Twig_Extensions_' => array($vendorDir . '/twig/extensions/lib') . 可以在autoload_namespaces.php文件中找到以下行: 'Twig_Extensions_' => array($vendorDir . '/twig/extensions/lib')

I tried adding a basic Twig_SimpleFilter the same way, it doesn't work either, so it seems the way I'm trying to add this is just plain wrong, yet it's the way I find everywhere... 我尝试以相同的方式添加基本的Twig_SimpleFilter ,但它也不起作用,因此看来我尝试添加此方法的方式是完全错误的,但这是我到处都能找到的方式...

The twig service is being redefined later on in the application boot process. 稍后在应用程序引导过程中重新定义树枝服务。 (See question comments for debugging steps). (有关调试步骤,请参阅问题注释)。 The first time twig is being created, you are extending it and adding the extension. 第一次创建树枝时,您要对其进行扩展并添加扩展。 Later on, twig is getting defined again , but this time no extension is added. 稍后, 又重新定义 twig,但是这次没有添加扩展名。

The second definition overrides the first which is the primary problem. 第二个定义覆盖了第一个定义,第一个定义是主要问题。 The confusing part is that the original extension never gets called. 令人困惑的部分是原始扩展名永远不会被调用。 This is because Silex doesn't actually call your extension function until the twig service is used . 这是因为Silex 在使用树枝服务之前实际上不会调用您的扩展函数。 Since you are overriding it before that happens, the extension function is never called. 由于在此之前您要覆盖它,因此永远不会调用扩展功能。 To debug, you called $app['twig'] immediately after you defined twig the first time and ensured the extension got run. 为了进行调试,您在首次定义twig并确保扩展已运行后立即调用$app['twig'] Through the process of elimination, that means that the twig service is getting overridden sometime later. 通过消除过程,这意味着分支服务将在以后的某个时间被覆盖。 You determined that that is in the config file. 您确定那是在配置文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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