Commit 944c118b authored by Fabien Potencier's avatar Fabien Potencier

added support for HTTP foundation Twig extension

parent 48a3fdc4
...@@ -61,6 +61,8 @@ some Symfony components and Twig. Add it as a dependency: ...@@ -61,6 +61,8 @@ some Symfony components and Twig. Add it as a dependency:
When present, the ``TwigServiceProvider`` will provide you with the following When present, the ``TwigServiceProvider`` will provide you with the following
additional capabilities: additional capabilities:
* **absolute_url()** and **relative_path()** Twig functions.
* **UrlGeneratorServiceProvider**: If you are using the * **UrlGeneratorServiceProvider**: If you are using the
``UrlGeneratorServiceProvider``, you will have access to the ``path()`` and ``UrlGeneratorServiceProvider``, you will have access to the ``path()`` and
``url()`` functions. You can find more information in the `Symfony Routing ``url()`` functions. You can find more information in the `Symfony Routing
......
...@@ -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