Skip to content

Added a new section to the page templating/global_vars using a EVListener #6794

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 9 commits into from
Closed
Changes from 6 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
25 changes: 25 additions & 0 deletions templating/global_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,28 @@ 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
a :ref:`Twig Extension <reference-dic-tags-twig-extension>` and return the
global variable as one of the entries in the ``getGlobals`` method.

.. note::

``getGlobals`` is deprecated as of Twig v1.23 and removed in v2.0.

Using an Event Listener Together with the @Template Annotation
--------------------------------------------------------------

If you're using the `@Template`_ annotation from `SensioFrameworkExtraBundle`_ you can hook
into ``kernel.view`` right before the template listener kicks in, take a look at the
dedicated page about :doc:`Event Listeners </event_dispatcher.html>`_.
Copy link

Choose a reason for hiding this comment

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

The syntax error is caused by the underscore at the end of the :doc: link.
Something like this is fine:

page about :doc:`/event_dispatcher`.


Here is an example of changing the parameters when you have your listener set up:

.. code-block:: php

public function onKernelView(GetResponseForControllerResultEvent $event)
{
$params = $event->getControllerResult();
$params['sadface'] = 'You need me everywhere!';
$event->setControllerResult($params);
}

.. _`@Template`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view
.. _`SensioFrameworkExtraBundle`: http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle
Copy link

Choose a reason for hiding this comment

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

Should point to current instead of master. Perhaps point to the annotations part directly:
http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#annotations-for-controllers