概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:41:27 | 234 次阅读 | 评论: 0 | 来源: 网络整理

Sometimes you want a variable to be accessible to all the templates you use. This is possible inside your app/config/config.yml file:

  • YAML
    # app/config/config.yml
    twig:
        # ...
        globals:
            ga_tracking: UA-xxxxx-x
    
  • XML
    <!-- app/config/config.xml -->
    <twig:config>
        <!-- ... -->
        <twig:global key="ga_tracking">UA-xxxxx-x</twig:global>
    </twig:config>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('twig', array(
         // ...
         'globals' => array(
             'ga_tracking' => 'UA-xxxxx-x',
         ),
    ));
    

Now, the variable ga_tracking is available in all Twig templates:

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

It’s that easy!

Using Service Container Parameters

You can also take advantage of the built-in Service的参数 system, which lets you isolate or reuse the value:

# app/config/parameters.yml
parameters:
    ga_tracking: UA-xxxxx-x
  • YAML
    # app/config/config.yml
    twig:
        globals:
            ga_tracking: "%ga_tracking%"
    
  • XML
    <!-- app/config/config.xml -->
    <twig:config>
        <twig:global key="ga_tracking">%ga_tracking%</twig:global>
    </twig:config>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('twig', array(
         'globals' => array(
             'ga_tracking' => '%ga_tracking%',
         ),
    ));
    

The same variable is available exactly as before.

Referencing Services

Instead of using static values, you can also set the value to a service. Whenever the global variable is accessed in the template, the service will be requested from the service container and you get access to that object.

注解

The service is not loaded lazily. In other words, as soon as Twig is loaded, your service is instantiated, even if you never use that global variable.

To define a service as a global Twig variable, prefix the string with @. This should feel familiar, as it’s the same syntax you use in service configuration.

  • YAML
    # app/config/config.yml
    twig:
        # ...
        globals:
            user_management: "@acme_user.user_management"
    
  • XML
    <!-- app/config/config.xml -->
    <twig:config>
        <!-- ... -->
        <twig:global key="user_management">@acme_user.user_management</twig:global>
    </twig:config>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('twig', array(
         // ...
         'globals' => array(
             'user_management' => '@acme_user.user_management',
         ),
    ));
    

Using a Twig Extension

If the global variable you want to set is more complicated - say an object - then you won’t be able to use the above method. Instead, you’ll need to create a Twig Extension and return the global variable as one of the entries in the getGlobals method.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务