@@ -41,7 +41,12 @@ Now, the variable ``ga_tracking`` is available in all Twig templates:
41
41
42
42
<p>The google tracking code is: {{ ga_tracking }}</p>
43
43
44
- It's that easy! You can also take advantage of the built-in :ref: `book-service-container-parameters `
44
+ It's that easy!
45
+
46
+ Using Service Container Parameters
47
+ ----------------------------------
48
+
49
+ You can also take advantage of the built-in :ref: `book-service-container-parameters `
45
50
system, which lets you isolate or reuse the value:
46
51
47
52
.. code-block :: yaml
@@ -77,8 +82,52 @@ system, which lets you isolate or reuse the value:
77
82
78
83
The same variable is available exactly as before.
79
84
80
- More Complex Global Variables
81
- -----------------------------
85
+ Referencing Services
86
+ --------------------
87
+
88
+ Instead of using static values, you can also set the value to a service.
89
+ Whenever the global variable is accessed in the template, the service will be
90
+ requested from the service container and you get access to that object.
91
+
92
+ .. note ::
93
+
94
+ The service is not loaded lazily. In other words, as soon as Twig is
95
+ loaded, your service is instantiated, even if you never use that global
96
+ variable.
97
+
98
+ To define a service as a global Twig variable, prefix the string with ``@ ``.
99
+ This should feel familiar, as it's the same syntax you use in service configuration.
100
+
101
+ .. configuration-block ::
102
+
103
+ .. code-block :: yaml
104
+
105
+ # app/config/config.yml
106
+ twig :
107
+ # ...
108
+ globals :
109
+ user_management : " @acme_user.user_management"
110
+
111
+ .. code-block :: xml
112
+
113
+ <!-- app/config/config.xml -->
114
+ <twig : config ...>
115
+ <!-- ... -->
116
+ <twig : global key =" user_management" >@acme_user.user_management</twig : global >
117
+ </twig : config >
118
+
119
+ .. code-block :: php
120
+
121
+ // app/config/config.php
122
+ $container->loadFromExtension('twig', array(
123
+ // ...
124
+ 'globals' => array(
125
+ 'user_management' => '@acme_user.user_management',
126
+ ),
127
+ ));
128
+
129
+ Using a Twig Extension
130
+ ----------------------
82
131
83
132
If the global variable you want to set is more complicated - say an object -
84
133
then you won't be able to use the above method. Instead, you'll need to create
0 commit comments