-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Documented services as global variables #2865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,50 @@ Now, the variable ``ga_tracking`` is available in all Twig templates: | |
|
||
<p>The google tracking code is: {{ ga_tracking }}</p> | ||
|
||
It's that easy! You can also take advantage of the built-in :ref:`book-service-container-parameters` | ||
It's that easy! | ||
|
||
Referencing Services | ||
-------------------- | ||
|
||
Instead of using static values, you can also set the value to a service. | ||
Whenever the global variabele is accessed in the template, the service will be | ||
requested from the service container and you get access to that object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence is misleading: it let me think that the service is lazy-loaded while it is actually retrieved from the container when instantiating the Twig_Environment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A then I understood it wrongly, will fix it. |
||
|
||
This is done by prefixing the string with ``@``, which you already know from | ||
injecting a service: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
twig: | ||
# ... | ||
globals: | ||
user_management: "@acme_user.user_management" | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<twig:config ...> | ||
<!-- ... --> | ||
<twig:global key="user_management">@acme_user.user_management</twig:global> | ||
</twig:config> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('twig', array( | ||
// ... | ||
'globals' => array( | ||
'user_management' => '@acme_user.user_management', | ||
), | ||
)); | ||
|
||
Using Service Container Parameters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, using parameters as global variables is more common than using services so it should come before. |
||
---------------------------------- | ||
|
||
You can also take advantage of the built-in :ref:`book-service-container-parameters` | ||
system, which lets you isolate or reuse the value: | ||
|
||
.. code-block:: yaml | ||
|
@@ -77,8 +120,8 @@ system, which lets you isolate or reuse the value: | |
|
||
The same variable is available exactly as before. | ||
|
||
More Complex Global Variables | ||
----------------------------- | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable