Skip to content

Commit 546b1dd

Browse files
committed
Merge pull request #992 from ChrisTickner/app_global_variable
[Book] [Templating] Added app global variable section
2 parents 30b2983 + 6fcab8c commit 546b1dd

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

book/templating.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,45 @@ is by default "web").
871871
The end result is a page that includes both the ``main.css`` and ``contact.css``
872872
stylesheets.
873873

874+
Global Template Variables
875+
-------------------------
876+
877+
During each request, Symfony2 will set a global template variable ``app``
878+
in both Twig and PHP template engines by default. The ``app`` variable
879+
is a :class:`Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables`
880+
instance which will give you access to some application specific variables
881+
automatically:
882+
883+
* ``app.security`` - The security context.
884+
* ``app.user`` - The current user object.
885+
* ``app.request`` - The request object.
886+
* ``app.session`` - The session object.
887+
* ``app.environment`` - The current environment (dev, prod, etc).
888+
* ``app.debug`` - True if in debug mode. False otherwise.
889+
890+
.. configuration-block::
891+
892+
.. code-block:: html+jinja
893+
894+
<p>Username: {{ app.user.username }}</p>
895+
{% if app.debug %}
896+
<p>Request method: {{ app.request.method }}</p>
897+
<p>Application Environment: {{ app.environment }}</p>
898+
{% endif %}
899+
900+
.. code-block:: html+php
901+
902+
<p>Username: <?php echo $app->getUser()->getUsername() ?></p>
903+
<?php if ($app->getDebug()): ?>
904+
<p>Request method: <?php echo $app->getRequest()->getMethod() ?></p>
905+
<p>Application Environment: <?php echo $app->getEnvironment() ?></p>
906+
<?php endif; ?>
907+
908+
.. tip::
909+
910+
You can add your own global template variables. See the cookbook example
911+
on :doc:`Global Variables</cookbook/templating/global_variables>`.
912+
874913
.. index::
875914
single: Templating; The templating service
876915

0 commit comments

Comments
 (0)