Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions cookbook/templating/global_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable

requested from the service container and you get access to that object.
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
Expand Down