如何在 Symfony2 Twig 模板中获取配置参数 [英] How to get config parameters in Symfony2 Twig Templates

查看:31
本文介绍了如何在 Symfony2 Twig 模板中获取配置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Symfony2 Twig 模板.我想在这个树枝模板(一个版本号)中输出一个配置参数的值.因此我定义了这样的配置参数:

I have a Symfony2 Twig template. I want to output the value of a config parameter in this twig template (a version number). Therefore I defined the config parameter like this:

parameters:
    app.version: 0.1.0

我可以在控制器中使用这个配置参数,但我不知道如何在我的 Twig 模板中获取它.

I'm able to use this config parameter in Controllers but I have no clue how to get it in my Twig template.

推荐答案

您可以在 twig globals 配置部分:

You can use parameter substitution in the twig globals section of the config:

参数配置:

parameters:
    app.version: 0.1.0

树枝配置:

twig:
    globals:
        version: '%app.version%'

树枝模板:

{{ version }}

此方法提供的好处是允许您也使用 ContainerAware 类中的参数,使用:

This method provides the benefit of allowing you to use the parameter in ContainerAware classes as well, using:

$container->getParameter('app.version');

这篇关于如何在 Symfony2 Twig 模板中获取配置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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