Commit ee118729 authored by Fabien Potencier's avatar Fabien Potencier

feature #1220 added support for HTTP foundation Twig extension (fabpot)

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

Discussion
----------

added support for HTTP foundation Twig extension

Commits
-------

944c118b added support for HTTP foundation Twig extension
parents 86a1e29d 944c118b
...@@ -72,6 +72,15 @@ additional capabilities: ...@@ -72,6 +72,15 @@ additional capabilities:
{{ path('hello', {name: 'Fabien'}) }} {{ path('hello', {name: 'Fabien'}) }}
{{ url('hello', {name: 'Fabien'}) }} {# generates the absolute url http://example.org/hello/Fabien #} {{ url('hello', {name: 'Fabien'}) }} {# generates the absolute url http://example.org/hello/Fabien #}
* Access to the ``absolute_url()`` and ``relative_path()`` Twig functions.
* **UrlGeneratorServiceProvider**: If you are using the
``UrlGeneratorServiceProvider``, you will have access to the ``path()`` and
``url()`` functions. You can find more information in the `Symfony Routing
documentation
<http://symfony.com/doc/current/book/routing.html#generating-urls-from-a-template>`_.
>>>>>>> pull/1220
* **TranslationServiceProvider**: If you are using the * **TranslationServiceProvider**: If you are using the
``TranslationServiceProvider``, you will get the ``trans()`` and ``TranslationServiceProvider``, you will get the ``trans()`` and
``transchoice()`` functions for translation in Twig templates. You can find ``transchoice()`` functions for translation in Twig templates. You can find
......
...@@ -17,6 +17,7 @@ use Symfony\Bridge\Twig\Extension\RoutingExtension; ...@@ -17,6 +17,7 @@ use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension; use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Extension\FormExtension; use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\SecurityExtension; use Symfony\Bridge\Twig\Extension\SecurityExtension;
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension; use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine; use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Form\TwigRenderer; use Symfony\Bridge\Twig\Form\TwigRenderer;
...@@ -52,6 +53,8 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -52,6 +53,8 @@ class TwigServiceProvider implements ServiceProviderInterface
} }
if (class_exists('Symfony\Bridge\Twig\Extension\RoutingExtension')) { if (class_exists('Symfony\Bridge\Twig\Extension\RoutingExtension')) {
$twig->addExtension(new HttpFoundationExtension($app['request_stack']));
if (isset($app['url_generator'])) { if (isset($app['url_generator'])) {
$twig->addExtension(new RoutingExtension($app['url_generator'])); $twig->addExtension(new RoutingExtension($app['url_generator']));
} }
......
...@@ -52,4 +52,19 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -52,4 +52,19 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
}; };
$this->assertEquals('foo', $app['twig.loader']->getSource('foo')); $this->assertEquals('foo', $app['twig.loader']->getSource('foo'));
} }
public function testHttpFoundationIntegration()
{
$app = new Application();
$app['request_stack']->push(Request::create('/dir1/dir2/file'));
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array(
'absolute' => '{{ absolute_url("foo.css") }}',
'relative' => '{{ relative_path("/dir1/foo.css") }}',
),
));
$this->assertEquals('http://localhost/dir1/dir2/foo.css', $app['twig']->render('absolute'));
$this->assertEquals('../foo.css', $app['twig']->render('relative'));
}
} }
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