3

我尝试将 Twig-extensions 加载到 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());
});

Pathes 似乎是正确的,Twig 本身工作正常。

任何的想法?

4

3 回答 3

7

在 Silex 1.3 中,您可以使用 Pimple 的 extend方法:

$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
    $twig->addExtension(new \My\Twig\Extension\SomeExtension($app));
    return $twig;
}));
于 2012-08-23T15:46:23.970 回答
2

在 Silex 2.0 中,首先注册 TwigServiceProvider

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/views',
));

然后使用Twig 自定义路径

您可以通过扩展twig服务在使用之前配置 Twig 环境

Twig Extensions 安装指南

$app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extensions_Extension_Text());
    return $twig;
});
于 2016-12-22T20:17:57.287 回答
1

原因很简单。PEAR 约定自动加载映射定义为“前缀”=>“路径”。您正在为 twig 扩展设置“Twig_”前缀,然后注册 twig 服务提供者,它将覆盖它,指向 twig 本身。

解决方案是使用“Twig_”以外的前缀,最好使用更具体的前缀。类似“Twig_Extensions_”的东西。

$app['autoloader']->registerPrefix('Twig_Extensions_', __DIR__.'/../vendor/twig-extensions/lib');

那应该解决它。

于 2011-11-20T00:13:45.633 回答