Silex 中的 Twig 扩展
我尝试将 Twig 扩展加载到 Silex 中,但得到:
找不到“Twig_Extensions_Extension_Text”
我首先在自动加载器中注册 Twig-Extensions:
$app['autoloader']->registerPrefixes(array( 'Twig_' => array(__DIR__.'/../vendor/Twig-extensions/fabpot/lib')));
然后注册 Twig:
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/../views',
'twig.class_path' => __DIR__ . '/../vendor/twig/lib',
));
并添加扩展。
$oldTwigConfiguration = isset($app['twig.configure']) ? $app['twig.configure']: function(){};
$app['twig.configure'] = $app->protect(function($twig) use ($oldTwigConfiguration) {
$oldTwigConfiguration($twig);
$twig->addExtension(new Twig_Extensions_Extension_Text());
});
路径似乎是正确的,并且 Twig 本身工作正常。
有什么想法吗?
I try to load Twig-extensions into Silex but get a:
'Twig_Extensions_Extension_Text' not found
I first register Twig-Extensions in the autoloader:
$app['autoloader']->registerPrefixes(array( 'Twig_' => array(__DIR__.'/../vendor/Twig-extensions/fabpot/lib')));
Then register Twig:
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/../views',
'twig.class_path' => __DIR__ . '/../vendor/twig/lib',
));
and add the Extension.
$oldTwigConfiguration = isset($app['twig.configure']) ? $app['twig.configure']: function(){};
$app['twig.configure'] = $app->protect(function($twig) use ($oldTwigConfiguration) {
$oldTwigConfiguration($twig);
$twig->addExtension(new Twig_Extensions_Extension_Text());
});
Pathes seem to be correct and Twig itself works fine.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Silex 1.3 中,您可以使用 Pimple 的 new
extend
方法:In Silex 1.3 you can use the new
extend
method of Pimple:原因很简单。 PEAR 约定自动加载映射定义为“前缀”=> “小路”。您正在为 twig 扩展设置“Twig_”前缀,然后注册 twig 服务提供者,它将覆盖它,指向 twig 本身。
解决方案是使用“Twig_”以外的前缀,最好是更具体的前缀。类似于“Twig_Extensions_”。
那应该解决它。
The reason is simple. PEAR-convention autoload mappings are defined as "prefix" => "path". You are setting the "Twig_" prefix for the twig extensions, then you register the twig service provider, which will override it, pointing to twig itself.
The solution is to use a prefix other than "Twig_", preferably something more specific. Something like "Twig_Extensions_".
That should fix it.
在 Silex 2.0 中,首先注册 TwigServiceProvider:
然后使用 < a href="http://silex.sensiolabs.org/doc/master/providers/twig.html#customization" rel="nofollow noreferrer">Twig 自定义路径
和 Twig Extensions 安装指南:
In Silex 2.0, first register the TwigServiceProvider:
Then use Twig Customization path
and Twig Extensions installation guide: