Skip to content

Commit fe86a77

Browse files
committed
Merge branch '2.1'
2 parents 601bb87 + 84be0e1 commit fe86a77

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
* :doc:`/cookbook/templating/global_variables`
134134
* :doc:`/cookbook/templating/PHP`
135135
* :doc:`/cookbook/templating/twig_extension`
136+
* :doc:`/cookbook/templating/render_without_controller`
136137

137138
* :doc:`/cookbook/testing/index`
138139

cookbook/templating/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Templating
77
global_variables
88
PHP
99
twig_extension
10+
render_without_controller
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
.. index::
2+
single: Templating; Render template without custom controller
3+
4+
How to render a Template without a custom Controller
5+
====================================================
6+
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.
12+
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:
16+
17+
.. configuration-block::
18+
19+
.. code-block:: yaml
20+
21+
acme_privacy:
22+
pattern: /privacy
23+
defaults:
24+
_controller: FrameworkBundle:Template:template
25+
template: 'AcmeBundle:Static:privacy.html.twig'
26+
27+
.. code-block:: xml
28+
29+
<?xml version="1.0" encoding="UTF-8" ?>
30+
31+
<routes xmlns="http://symfony.com/schema/routing"
32+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
34+
35+
<route id="acme_privacy" pattern="/privacy">
36+
<default key="_controller">FrameworkBundle:Template:template</default>
37+
<default key="template">AcmeBundle:Static:privacy.html.twig</default>
38+
</route>
39+
</routes>
40+
41+
.. code-block:: php
42+
43+
use Symfony\Component\Routing\RouteCollection;
44+
use Symfony\Component\Routing\Route;
45+
46+
$collection = new RouteCollection();
47+
$collection->add('acme_privacy', new Route('/privacy', array(
48+
'_controller' => 'FrameworkBundle:Template:template',
49+
'template' => 'AcmeBundle:Static:privacy.html.twig',
50+
)));
51+
52+
return $collection;
53+
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+
) ?>

0 commit comments

Comments
 (0)