Twig 扩展将在新的 Twig_Environment 中可用 [英] Twig extension to be available in new Twig_Environment

查看:67
本文介绍了Twig 扩展将在新的 Twig_Environment 中可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Symfony 2/Twig.我有一个 Twig 扩展,除其他外,它会根据用户偏好重载日期过滤器以输出自定义日期格式.

I'm using Symfony 2 / Twig. I have a Twig extension which, among other things, overloads the date filter to output custom date formats based on user preference.

app/config/services.yml:

app/config/services.yml:

twig.extension.static:
    class: %twig_static_extension%  # set elsewhere
    tags:
        - { name: twig.extension }

我需要初始化一个新的 Twig_Environment 以呈现数据库中的模板:

I'm needing to initialize a new Twig_Environment in order to render templates from the database:

控制器:

$env = new \Twig_Environment(new \Twig_Loader_String());

但我发现这个新环境不会自动包含扩展.

But I am finding that this new environment does not automatically include the extension.

当然,我可以像这样加载它:

Naturally, I can load it like so:

$env->addExtension(new FQCN\To\StaticExtension());

我可以做些什么来使扩展可用于所有可能使用的 Twig 环境?

Is there something I can do to make the extension available to all Twig environments that might be used?

推荐答案

您可以创建一个 Compiler Pass 并将其注入您的 bundle,请访问 TwigEnvironmentPass 来自 TwigBundle

You can create a Compiler Pass and inject it to your bundle, take a took at TwigEnvironmentPass from TwigBundle

这篇关于Twig 扩展将在新的 Twig_Environment 中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆