The TwigServiceProvider provides integration with the Twig template engine.
FormServiceProvider
is enabled). The
default theme is form_div_layout.html.twig
, but you can use the other
built-in themes: form_table_layout.html.twig
,
bootstrap_3_layout.html.twig
, and
bootstrap_3_horizontal_layout.html.twig
.Twig_Environment
instance. The main way of
interacting with Twig.twig.path
and the twig.templates
options. You can also replace the loader
completely.1 2 3 | $app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
));
|
Note
Add Twig as a dependency:
1 | composer require twig/twig
|
The Twig provider provides a twig
service that can render templates:
1 2 3 4 5 | $app->get('/hello/{name}', function ($name) use ($app) {
return $app['twig']->render('hello.twig', array(
'name' => $name,
));
});
|
Symfony provides a Twig bridge that provides additional integration between some Symfony components and Twig. Add it as a dependency:
1 | composer require symfony/twig-bridge
|
When present, the TwigServiceProvider
will provide you with the following
additional capabilities.
Access to the path()
and url()
functions. You can find more
information in the Symfony Routing documentation:
1 2 3 4 | {{ path('homepage') }}
{{ url('homepage') }} {# generates the absolute url http://example.org/ #}
{{ 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.
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.
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.
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.
When the Twig bridge is available, the global
variable refers to an
instance of AppVariable.
It gives access to the following methods:
1 2 3 4 5 6 7 8 9 10 11 | {# The current Request #}
{{ global.request }}
{# The current User (when security is enabled) #}
{{ global.user }}
{# The current Session #}
{{ global.session }}
{# The debug flag #}
{{ global.debug }}
|
A render
function is also registered to help you render another controller
from a template (available when the HttpFragment Service Provider is registered):
1 2 3 4 | {{ render(url('sidebar')) }}
{# or you can reference a controller directly without defining a route for it #}
{{ render(controller(controller)) }}
|
Note
You must prepend the app.request.baseUrl
to render calls to ensure
that the render works when deployed into a sub-directory of the docroot.
Note
Read the Twig reference for Symfony document to learn more about the various Twig functions.
Silex\Application\TwigTrait
adds the following shortcuts:
1 2 3 4 5 6 | return $app->render('index.html', ['name' => 'Fabien']);
$response = new Response();
$response->setTtl(10);
return $app->render('index.html', ['name' => 'Fabien'], $response);
|
1 2 3 4 | // stream a view
use Symfony\Component\HttpFoundation\StreamedResponse;
return $app->render('index.html', ['name' => 'Fabien'], new StreamedResponse());
|
1 | $content = $app->renderView('index.html', ['name' => 'Fabien']);
|
You can configure the Twig environment before using it by extending the
twig
service:
1 2 3 4 5 6 | $app->extend('twig', function($twig, $app) {
$twig->addGlobal('pi', 3.14);
$twig->addFilter('levenshtein', new \Twig_Filter_Function('levenshtein'));
return $twig;
});
|
For more information, check out the official Twig documentation.