Silex 中的 Twig 扩展

发布于 2024-12-16 21:59:56 字数 899 浏览 0 评论 0原文

我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏了南城 2024-12-23 21:59:56

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

$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
    $twig->addExtension(new \My\Twig\Extension\SomeExtension($app));
    return $twig;
}));

In Silex 1.3 you can use the new extend method of Pimple:

$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
    $twig->addExtension(new \My\Twig\Extension\SomeExtension($app));
    return $twig;
}));
漫漫岁月 2024-12-23 21:59:56

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

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

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

那应该解决它。

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_".

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

That should fix it.

書生途 2024-12-23 21:59:56

在 Silex 2.0 中,首先注册 TwigServiceProvider

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

然后使用 < a href="http://silex.sensiolabs.org/doc/master/providers/twig.html#customization" rel="nofollow noreferrer">Twig 自定义路径

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

Twig Extensions 安装指南

$app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extensions_Extension_Text());
    return $twig;
});

In Silex 2.0, first register the TwigServiceProvider:

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

Then use Twig Customization path

You can configure the Twig environment before using it by extending the twig service

and Twig Extensions installation guide:

$app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extensions_Extension_Text());
    return $twig;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文