Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit50aa6a5

Browse files
committed
Merge branch '2.2' into 2.3
2 parentsa78d1a4 +e203c40 commit50aa6a5

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

‎cookbook/templating/global_variables.rst

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ Now, the variable ``ga_tracking`` is available in all Twig templates:
4141

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

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`
4550
system, which lets you isolate or reuse the value:
4651

4752
..code-block::yaml
@@ -77,8 +82,52 @@ system, which lets you isolate or reuse the value:
7782
7883
The same variable is available exactly as before.
7984

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:globalkey="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+
----------------------
82131

83132
If the global variable you want to set is more complicated - say an object -
84133
then you won't be able to use the above method. Instead, you'll need to create

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp