Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
reverted app Twig variable change, introduced a new global one instead
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 15, 2015
1 parent 7b095c0 commit 020b313
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 43 deletions.
2 changes: 1 addition & 1 deletion doc/changelog.rst
Expand Up @@ -5,7 +5,7 @@ Changelog
------------------

* added support for the Symfony VarDumper Component
* [BC BREAK] The app variable exposed in Twig is now an AppVariable instance
* added a global Twig variable (an AppVariable instance)
* [BC BREAK] CSRF has been moved to a standalone provider (``form.secret`` is not available anymore)
* added support for the Symfony HttpFoundation Twig bridge extension
* added support for the Symfony Asset Component
Expand Down
11 changes: 2 additions & 9 deletions doc/providers/translation.rst
Expand Up @@ -183,15 +183,8 @@ Accessing translations in Twig templates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Once loaded, the translation service provider is available from within Twig
templates:

.. code-block:: jinja
{{ app.translator.trans('translation_key') }}
Moreover, when using the Twig bridge provided by Symfony (see
:doc:`TwigServiceProvider </providers/twig>`), you will be allowed to translate
strings in the Twig way:
templates when using the Twig bridge provided by Symfony (see
:doc:`TwigServiceProvider </providers/twig>`):

.. code-block:: jinja
Expand Down
72 changes: 42 additions & 30 deletions doc/providers/twig.rst
Expand Up @@ -51,6 +51,17 @@ Registering
composer require twig/twig
Usage
-----

The Twig provider provides a ``twig`` service that can render templates::

$app->get('/hello/{name}', function ($name) use ($app) {
return $app['twig']->render('hello.twig', array(
'name' => $name,
));
});

Symfony Components Integration
------------------------------

Expand All @@ -62,7 +73,7 @@ some Symfony components and Twig. Add it as a dependency:
composer require symfony/twig-bridge
When present, the ``TwigServiceProvider`` will provide you with the following
additional capabilities:
additional capabilities.

* Access to the ``path()`` and ``url()`` functions. You can find more
information in the `Symfony Routing documentation
Expand All @@ -77,53 +88,54 @@ additional capabilities:
* Access to the ``absolute_url()`` and ``relative_path()`` Twig functions.

* **TranslationServiceProvider**: If you are using the
``TranslationServiceProvider``, you will get the ``trans()`` and
``transchoice()`` functions for translation in Twig templates. You can find
more information in the `Symfony Translation documentation
<http://symfony.com/doc/current/book/translation.html#twig-templates>`_.
Translations Support
~~~~~~~~~~~~~~~~~~~~

* **FormServiceProvider**: If you are using the ``FormServiceProvider``, you
will get a set of helpers for working with forms in templates. You can find
more information in the `Symfony Forms reference
<http://symfony.com/doc/current/reference/forms/twig_reference.html>`_.
If you are using the ``TranslationServiceProvider``, you will get the
``trans()`` and ``transchoice()`` functions for translation in Twig templates.
You can find more information in the `Symfony Translation documentation
<http://symfony.com/doc/current/book/translation.html#twig-templates>`_.

* **SecurityServiceProvider**: If you are using the
``SecurityServiceProvider``, you will have access to the ``is_granted()``
function in templates. You can find more information in the `Symfony
Security documentation
<http://symfony.com/doc/current/book/security.html#access-control-in-templates>`_.
Form Support
~~~~~~~~~~~~

Usage
-----
If you are using the ``FormServiceProvider``, you will get a set of helpers for
working with forms in templates. You can find more information in the `Symfony
Forms reference
<http://symfony.com/doc/current/reference/forms/twig_reference.html>`_.

The Twig provider provides a ``twig`` service::
Security Support
~~~~~~~~~~~~~~~~

$app->get('/hello/{name}', function ($name) use ($app) {
return $app['twig']->render('hello.twig', array(
'name' => $name,
));
});
If you are using the ``SecurityServiceProvider``, you will have access to the
``is_granted()`` function in templates. You can find more information in the
`Symfony Security documentation
<http://symfony.com/doc/current/book/security.html#access-control-in-templates>`
_.

This will render a file named ``views/hello.twig``.
Global Variable
~~~~~~~~~~~~~~~

In any Twig template, the ``app`` variable refers to an instance of
`AppVariable <http://api.symfony.com/master/Symfony/Bridge/Twig/AppVariable.html >`_.
When the Twig bridge is available, the ``global`` variable refers to an
instance of `AppVariable <http://api.symfony.com/master/Symfony/Bridge/Twig/AppVariable.html >`_.
It gives access to the following methods:

.. code-block:: jinja
{# The current Request #}
{{ app.request }}
{{ global.request }}
{# The current User (when security is enabled) #}
{{ app.user }}
{{ global.user }}
{# The current Session #}
{{ app.session }}
{{ global.session }}
{# The debug flag #}
{{ app.debug }}
{{ global.debug }}
Rendering a Controller
~~~~~~~~~~~~~~~~~~~~~~

A ``render`` function is also registered to help you render another controller
from a template (available when the `HttpFragment Service Provider </providers/http_fragment.rst>`
Expand Down
6 changes: 5 additions & 1 deletion src/Silex/Provider/TwigServiceProvider.php
Expand Up @@ -62,13 +62,17 @@ public function register(Container $app)
);

$twig = $app['twig.environment_factory']($app);
$twig->addGlobal('app', $app['twig.app_variable']);
// registered for BC, but should not be used anymore
// deprecated and should probably be removed in Silex 3.0
$twig->addGlobal('app', $app);

if ($app['debug']) {
$twig->addExtension(new \Twig_Extension_Debug());
}

if (class_exists('Symfony\Bridge\Twig\Extension\RoutingExtension')) {
$twig->addGlobal('global', $app['twig.app_variable']);

if (isset($app['request_stack'])) {
$twig->addExtension(new HttpFoundationExtension($app['request_stack']));
$twig->addExtension(new RoutingExtension($app['url_generator']));
Expand Down
4 changes: 2 additions & 2 deletions tests/Silex/Tests/Provider/TwigServiceProviderTest.php
Expand Up @@ -84,13 +84,13 @@ public function testAssetIntegration()
$this->assertEquals('/foo.css?1', $app['twig']->render('hello'));
}

public function testAppVariable()
public function testGlobalVariable()
{
$app = new Application();
$app['request_stack']->push(Request::create('/?name=Fabien'));

$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('hello' => '{{ app.request.get("name") }}'),
'twig.templates' => array('hello' => '{{ global.request.get("name") }}'),
));

$this->assertEquals('Fabien', $app['twig']->render('hello'));
Expand Down

0 comments on commit 020b313

Please sign in to comment.