Skip to content

Commit dd1a12a

Browse files
author
Chris Tickner
committed
[Book] [Templating] Added app global variable section
1 parent cb29085 commit dd1a12a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

book/templating.rst

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

874+
The ``app`` Global Variable
875+
---------------------------
876+
877+
Symfony2 will set a global template variable ``app`` on every request in
878+
both Twig and PHP template engines by default. The ``app`` variable will
879+
give you access to some application specific variables automatically:
880+
881+
* ``app.security`` - The security context service.
882+
* ``app.user`` - The current user object. Equivalent to ``app.security.token.user``.
883+
* ``app.request`` - The request object.
884+
* ``app.session`` - The session object. Equivalent to ``app.request.session``.
885+
* ``app.environment`` - The current environment (dev, prod, etc)
886+
* ``app.debug`` - True if in debug mode. False otherwise.
887+
888+
.. configuration-block::
889+
890+
.. code-block:: html+jinja
891+
892+
{% if app.debug %}
893+
<p>Username: {{ app.user.username }}</p>
894+
<p>Request method: {{ app.request.method }}</p>
895+
<p>Application Environment: {{ app.environment }}</p>
896+
{% endif %}
897+
898+
.. code-block:: html+php
899+
<?php if ($app->getDebug()): ?>
900+
<p>Username: <?php echo $app->getUser()->getUsername() ?></p>
901+
<p>Request method: <?php echo $app->getRequest()->getMethod() ?></p>
902+
<p>Application Environment: <?php echo $app->getEnvironment() ?></p>
903+
<?php endif; ?>
904+
874905
.. index::
875906
single: Templating; The templating service
876907

0 commit comments

Comments
 (0)