Skip to content

Commit 42b698d

Browse files
committed
[#2002] Re-adding the caching information to the rendering a template without a controller doc
1 parent fe86a77 commit 42b698d

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

cookbook/templating/render_without_controller.rst

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ template you've passed as the ``template`` default value.
5757
You can of course also use this trick when rendering embedded controllers
5858
from within a template. But since the purpose of rendering a controller from
5959
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.
60+
this is probably only useful if you'd like to cache this page partial (see
61+
:ref:`cookbook-templating-no-controller-caching`).
6262

6363
.. configuration-block::
6464

@@ -71,3 +71,67 @@ which will become available in Symfony 2.2.
7171
<?php echo $view['actions']->render(
7272
$view['router']->generate('acme_privacy', array(), true)
7373
) ?>
74+
75+
.. _cookbook-templating-no-controller-caching:
76+
77+
Caching the static Template
78+
---------------------------
79+
80+
.. versionadded:: 2.2
81+
The ability to cache templates rendered via ``FrameworkBundle:Template:template``
82+
is new in Symfony 2.2.
83+
84+
Since templates that are rendered in this way are typically static, it might
85+
make sense to cache them. Fortunately, this is easy! By configuring a few
86+
other variables in your route, you can control exactly how your page is cached:
87+
88+
.. configuration-block::
89+
90+
.. code-block:: yaml
91+
92+
acme_privacy:
93+
pattern: /privacy
94+
defaults:
95+
_controller: FrameworkBundle:Template:template
96+
template: 'AcmeBundle:Static:privacy.html.twig'
97+
maxAge: 86400
98+
sharedMaxAge: 86400
99+
100+
.. code-block:: xml
101+
102+
<?xml version="1.0" encoding="UTF-8" ?>
103+
104+
<routes xmlns="http://symfony.com/schema/routing"
105+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
106+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
107+
108+
<route id="acme_privacy" pattern="/privacy">
109+
<default key="_controller">FrameworkBundle:Template:template</default>
110+
<default key="template">AcmeBundle:Static:privacy.html.twig</default>
111+
<default key="maxAge">86400</default>
112+
<default key="sharedMaxAge">86400</default>
113+
</route>
114+
</routes>
115+
116+
.. code-block:: php
117+
118+
use Symfony\Component\Routing\RouteCollection;
119+
use Symfony\Component\Routing\Route;
120+
121+
$collection = new RouteCollection();
122+
$collection->add('acme_privacy', new Route('/privacy', array(
123+
'_controller' => 'FrameworkBundle:Template:template',
124+
'template' => 'AcmeBundle:Static:privacy.html.twig',
125+
'maxAge' => 86400,
126+
'sharedMaxAge' => 86400,
127+
)));
128+
129+
return $collection;
130+
131+
The ``maxAge`` and ``sharedMaxAge`` values are used to modify the Response
132+
object created in the controller. For more information on caching, see
133+
:doc:`/book/http_cache`.
134+
135+
There is also a ``private`` variable (not shown here). By default, the Response
136+
will be made public, as long as ``maxAge`` or ``sharedMaxAge`` are passed.
137+
If set to ``true``, the Response will be marked as private.

0 commit comments

Comments
 (0)