From 494c61eca6860e65cc84314d2db1cfcc9ca77336 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Tue, 30 Jul 2013 14:29:01 +0200 Subject: [PATCH] Documented services as global vars --- cookbook/templating/global_variables.rst | 49 ++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/cookbook/templating/global_variables.rst b/cookbook/templating/global_variables.rst index 4067cfb40fb..11cce2f0c2c 100644 --- a/cookbook/templating/global_variables.rst +++ b/cookbook/templating/global_variables.rst @@ -41,7 +41,50 @@ Now, the variable ``ga_tracking`` is available in all Twig templates:

The google tracking code is: {{ ga_tracking }}

-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. + +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 + + + + + @acme_user.user_management + + + .. code-block:: php + + // app/config/config.php + $container->loadFromExtension('twig', array( + // ... + 'globals' => array( + 'user_management' => '@acme_user.user_management', + ), + )); + +Using Service Container Parameters +---------------------------------- + +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