11..index ::
22 single: Templating; Render template without custom controller
33
4- How to render atemplate without a customcontroller
4+ How to render aTemplate without a customController
55====================================================
66
7- This guide explains how to render a template within another template and
8- how to configure a page without a custom controller.
7+ Usually, when you need to create a page, you need to create a controller
8+ and render a template from within that controller. But if you're rendering
9+ a simple template that doesn't need any data passed into it, you can avoid
10+ creating the controller entirely, by using the built-in ``FrameworkBundle:Template:template ``
11+ controller.
912
10- The intention is, that there may be page in your application, that doesn't
11- need a controller, because there is no action associated with them.
12-
13- Rendering a template in twig:
14-
15- ..code-block ::jinja
16-
17- {% render "FrameworkBundle:Template:template" with {template: 'AcmeBundle::static.html.twig'} %}
18-
19- Directly routing to a template without custom controller with additional
20- caching parameters:
13+ For example, suppose you want to render a ``AcmeBundle:Static:privacy.html.twig ``
14+ template, which doesn't require that any variables are passed to it. You
15+ can do this without creating a controller:
2116
2217..configuration-block ::
2318
2419 ..code-block ::yaml
2520
26- acme_static :
27- pattern :/static
21+ acme_privacy :
22+ pattern :/privacy
2823defaults :
2924_controller :FrameworkBundle:Template:template
30- template :' AcmeBundle::static.html.twig'
31- maxAge :86400
32- sharedMaxAge :86400
25+ template :' AcmeBundle:Static:privacy.html.twig'
3326
3427 ..code-block ::xml
3528
@@ -39,11 +32,9 @@ caching parameters:
3932xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4033xsi : schemaLocation =" http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd" >
4134
42- <route id =" acme_static " pattern =" /static " >
35+ <route id =" acme_privacy " pattern =" /privacy " >
4336 <default key =" _controller" >FrameworkBundle:Template:template</default >
44- <default key =" template" >AcmeBundle::static.html.twig</default >
45- <default key =" maxAge" >86400</default >
46- <default key =" sharedMaxAge" >86400</default >
37+ <default key =" template" >AcmeBundle:Static:privacy.html.twig</default >
4738 </route >
4839 </routes >
4940
@@ -53,15 +44,30 @@ caching parameters:
5344 use Symfony\Component\Routing\Route;
5445
5546 $collection = new RouteCollection();
56- $collection->add('acme_static ', new Route('/static ', array(
47+ $collection->add('acme_privacy ', new Route('/privacy ', array(
5748 '_controller' => 'FrameworkBundle:Template:template',
58- 'template' => 'Acmebundle::static.html.twig',
59- 'maxAge' => 86400,
60- 'sharedMaxAge' => 86400,
49+ 'template' => 'AcmeBundle:Static:privacy.html.twig',
6150 )));
6251
6352 return $collection;
6453
65- By default no caching headers were set. If you want to disable proxy
66- caching, but want to keep browser caching enabled, set ``private `` to
67- ``false `` explictly.
54+ The ``FrameworkBundle:Template:template `` controller will simply render whatever
55+ template you've passed as the ``template `` default value.
56+
57+ You can of course also use this trick when rendering embedded controllers
58+ from within a template. But since the purpose of rendering a controller from
59+ within a template is typically to prepare some data in a custom controller,
60+ this probably isn't useful, except to easily cache static partials, a feature
61+ which will become available in Symfony 2.2.
62+
63+ ..configuration-block ::
64+
65+ ..code-block ::html+jinja
66+
67+ {% render url('acme_privacy') %}
68+
69+ ..code-block ::html+php
70+
71+ <?php echo $view['actions']->render(
72+ $view['router']->generate('acme_privacy', array(), true)
73+ ) ?>