Commit 8d593f09 authored by Fabien Potencier's avatar Fabien Potencier

feature #1231 changed the app variable in Twig to be an instance of Symfony AppVariable (fabpot)

This PR was merged into the 2.0.x-dev branch.

Discussion
----------

changed the app variable in Twig to be an instance of Symfony AppVariable

That's more consistent with Symfony.

Commits
-------

0d10c5ce changed the app variable in Twig to be an instance of Symfony AppVariable
parents c2a9015e 0d10c5ce
......@@ -4,6 +4,7 @@ Changelog
2.0.0 (2015-XX-XX)
------------------
* [BC BREAK] The app variable exposed in Twig is now 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
......
......@@ -111,13 +111,23 @@ The Twig provider provides a ``twig`` service::
This will render a file named ``views/hello.twig``.
In any Twig template, the ``app`` variable refers to the Application object.
So you can access any service from within your view. For example to access
``$app['request']->getHost()``, just put this in your template:
In any Twig template, the ``app`` 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
{{ app.request.host }}
{# The current Request #}
{{ app.request }}
{# The current User (when security is enabled) #}
{{ app.user }}
{# The current Session #}
{{ app.session }}
{# The debug flag #}
{{ app.debug }}
A ``render`` function is also registered to help you render another controller
from a template:
......
......@@ -13,6 +13,7 @@ namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
......@@ -37,6 +38,17 @@ class TwigServiceProvider implements ServiceProviderInterface
$app['twig.path'] = array();
$app['twig.templates'] = array();
$app['twig.app_variable'] = function ($app) {
$var = new AppVariable();
if (isset($app['security.token_storage'])) {
$var->setTokenStorage($app['security.token_storage']);
}
$var->setRequestStack($app['request_stack']);
$var->setDebug($app['debug']);
return $var;
};
$app['twig'] = function ($app) {
$app['twig.options'] = array_replace(
array(
......@@ -47,7 +59,7 @@ class TwigServiceProvider implements ServiceProviderInterface
);
$twig = $app['twig.environment_factory']($app);
$twig->addGlobal('app', $app);
$twig->addGlobal('app', $app['twig.app_variable']);
if (isset($app['debug']) && $app['debug']) {
$twig->addExtension(new \Twig_Extension_Debug());
......
......@@ -81,4 +81,16 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/foo.css?1', $app['twig']->render('hello'));
}
public function testAppVariable()
{
$app = new Application();
$app['request_stack']->push(Request::create('/?name=Fabien'));
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('hello' => '{{ app.request.get("name") }}'),
));
$this->assertEquals('Fabien', $app['twig']->render('hello'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment