首页 文章

如何在Symfony2 Twig模板中获取配置参数

提问于
浏览
155

我有一个Symfony2 Twig模板 . 我想在此twig模板中输出config参数的值(版本号) . 因此我定义了config参数,如下所示:

parameters:
    app.version: 0.1.0

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

8 回答

  • 380

    很容易,您可以在配置文件中定义:

    twig:
        globals:
            version: "0.1.0"
    

    并使用 . 在模板中访问它

    {{ version }}
    

    否则,它必须是具有Twig扩展名的方式来公开您的参数 .

  • 12

    您可以在配置的twig globals部分中使用参数替换:

    参数配置:

    parameters:
        app.version: 0.1.0
    

    Twig配置:

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

    树枝模板:

    {{ version }}
    

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

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

    您还可以利用内置的Service Parameters系统,该系统允许您隔离或重用该值:

    # app/config/parameters.yml
    parameters:
        ga_tracking: UA-xxxxx-x
    
    # app/config/config.yml
    twig:
        globals:
            ga_tracking: "%ga_tracking%"
    

    现在,变量 ga_tracking 在所有Twig模板中都可用:

    <p>The google tracking code is: {{ ga_tracking }}</p>
    

    该参数也可在控制器内使用:

    $this->container->getParameter('ga_tracking');
    

    您还可以将服务定义为全局Twig变量(Symfony2.2):

    # app/config/config.yml
    twig:
        # ...
        globals:
            user_management: "@acme_user.user_management"
    

    http://symfony.com/doc/current/templating/global_variables.html

    如果你想要设置的全局变量更复杂 - 比如一个对象 - 那么你就赢了't be able to use the above method. Instead, you' ll需要create a Twig Extension并将全局变量作为getGlobals方法中的一个条目返回 .

  • -3

    在较新版本的Symfony2上(使用 parameters.yml 而不是 parameters.ini ),您可以存储对象或数组而不是键值对,因此您可以通过以下方式管理全局变量:

    config.yml(仅编辑一次):

    # app/config/config.yml
    twig:
      globals:
        project: %project%
    

    parameters.yml:

    # app/config/parameters.yml
    project:
      name:       myproject.com
      version:    1.1.42
    

    然后在twig文件中,您可以使用 {{ project.version }}{{ project.name }} .

    注意:我个人不喜欢将内容添加到 app ,只是因为's the Symfony'的变量,我不知道将来会存储什么 .

  • 1

    上面给出的ans是正确的,并且工作正常 . 我以不同的方式使用 .

    config.yml

    imports:
        - { resource: parameters.yml }
        - { resource: security.yml }
        - { resource: app.yml }
        - { resource: app_twig.yml }
    

    app.yml

    parameters:
      app.version:           1.0.1
    

    app_twig.yml

    twig:
      globals:
        version: %app.version%
    

    Inside controller:

    $application_version = $this->container->getParameter('app.version');
    // Here using app.yml
    

    Inside template/twig file:

    Project version {{ version }}!
    {#  Here using app_twig.yml content.  #}
    {#  Because in controller we used $application_version  #}
    

    使用控制器输出:

    Controller:

    public function indexAction() {
            $application_version = $this->container->getParameter('app.version');
            return array('app_version' => $application_version);
        }
    

    template/twig file :

    Project version {{ app_version }}
    

    我提到了不同的更好理解 .

  • 16

    使用Twig扩展,您可以创建 parameter Twig功能:

    {{ parameter('jira_host') }}
    

    TwigExtension.php:

    class TwigExtension extends \Twig_Extension
    {
        public $container;
    
        public function getFunctions()
        {
            return [
                new \Twig_SimpleFunction('parameter', function($name)
                {
                    return $this->container->getParameter($name);
                })
            ];
        }
    
    
        /**
         * Returns the name of the extension.
         *
         * @return string The extension name
         */
        public function getName()
        {
            return 'iz';
        }
    }
    

    service.yml:

    iz.twig.extension:
        class: IzBundle\Services\TwigExtension
        properties:
          container: "@service_container"
        tags:
          - { name: twig.extension }
    
  • 20

    您可以简单地将控制器中的 $this->getParameter('app.version') 绑定到twig param,然后再渲染它 .

  • 84

    在confing.yml

    # app/config/config.yml
    twig:
      globals:
        version: '%app.version%'
    

    在Twig视图中

    # twig view
    {{ version }}
    

相关问题