Skip to content

Commit

Permalink
feature silexphp#1220 added support for HTTP foundation Twig extensio…
Browse files Browse the repository at this point in the history
…n (fabpot)

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

Discussion
----------

added support for HTTP foundation Twig extension

Commits
-------

944c118 added support for HTTP foundation Twig extension
  • Loading branch information
fabpot committed Aug 23, 2015
2 parents 86a1e29 + 944c118 commit ee11872
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions doc/providers/twig.rst
Expand Up @@ -72,6 +72,15 @@ additional capabilities:
{{ path('hello', {name: '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``, you will get the ``trans()`` and
``transchoice()`` functions for translation in Twig templates. You can find
Expand Down
3 changes: 3 additions & 0 deletions src/Silex/Provider/TwigServiceProvider.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\SecurityExtension;
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Form\TwigRenderer;
Expand Down Expand Up @@ -52,6 +53,8 @@ public function register(Container $app)
}

if (class_exists('Symfony\Bridge\Twig\Extension\RoutingExtension')) {
$twig->addExtension(new HttpFoundationExtension($app['request_stack']));

if (isset($app['url_generator'])) {
$twig->addExtension(new RoutingExtension($app['url_generator']));
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Silex/Tests/Provider/TwigServiceProviderTest.php
Expand Up @@ -52,4 +52,19 @@ public function testLoaderPriority()
};
$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'));
}
}

0 comments on commit ee11872

Please sign in to comment.