Skip to content

Commit c2d0b99

Browse files
committed
made changes to take into account the new usage of the different content rendering strategies
1 parent 6bd16c2 commit c2d0b99

File tree

7 files changed

+106
-166
lines changed

7 files changed

+106
-166
lines changed

book/_security-2012-6431.rst.inc

Lines changed: 0 additions & 9 deletions
This file was deleted.

book/http_cache.rst

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -881,58 +881,37 @@ matter), Symfony2 uses the standard ``render`` helper to configure ESI tags:
881881

882882
.. code-block:: jinja
883883
884-
{% render url('latest_news', { 'max': 5 }), {'standalone': true} %}
884+
{# you can use a controller reference #}
885+
{{ render_esi(controller('...:news', { 'max': 5 })) }}
886+
887+
{# ... or a URL #}
888+
{{ render_esi(url('latest_news', { 'max': 5 })) }}
885889
886890
.. code-block:: php
887891
892+
<?php echo $view['actions']->render(
893+
new ControllerReference('...:news', array('max' => 5)),
894+
array('strategy' => 'esi'))
895+
?>
896+
888897
<?php echo $view['actions']->render(
889898
$view['router']->generate('latest_news', array('max' => 5), true),
890-
array('standalone' => true)
899+
array('strategy' => 'esi')
891900
) ?>
892901
893-
.. include:: /book/_security-2012-6431.rst.inc
894-
895-
The ``render`` tag takes the absolute url to the embedded action. This means
896-
that you need to define a new route to the controller that you're embedding:
897-
898-
.. code-block:: yaml
899-
900-
# app/config/routing.yml
901-
latest_news:
902-
pattern: /esi/latest-news/{max}
903-
defaults: { _controller: AcmeNewsBundle:News:news }
904-
requirements: { max: \d+ }
905-
906-
.. caution::
907-
908-
Unless you want this URL to be accessible to the outside world, you
909-
should use Symfony's firewall to secure it (by allowing access to your
910-
reverse proxy's IP range). See the :ref:`Securing by IP<book-security-securing-ip>`
911-
section of the :doc:`Security Chapter </book/security>` for more information
912-
on how to do this.
913-
914-
.. tip::
902+
By using the ``esi`` rendering strategy (via the ``render_esi`` Twig
903+
function), you tell Symfony2 that the action should be rendered as an ESI tag.
904+
You might be wondering why you would want to use a helper instead of just
905+
writing the ESI tag yourself. That's because using a helper makes your
906+
application work even if there is no gateway cache installed.
915907

916-
The best practice is to mount all your ESI urls on a single prefix (e.g.
917-
``/esi``) of your choice. This has two main advantages. First, it eases
918-
the management of ESI urls as you can easily identify the routes used for ESI.
919-
Second, it eases security management since securing all urls starting
920-
with the same prefix is easier than securing each individual url. See
921-
the above note for more details on securing ESI URLs.
922-
923-
By setting ``standalone`` to ``true`` in the ``render`` Twig tag, you tell
924-
Symfony2 that the action should be rendered as an ESI tag. You might be
925-
wondering why you would want to use a helper instead of just writing the ESI tag
926-
yourself. That's because using a helper makes your application work even if
927-
there is no gateway cache installed.
928-
929-
When standalone is ``false`` (the default), Symfony2 merges the included page
930-
content within the main one before sending the response to the client. But
931-
when standalone is ``true``, *and* if Symfony2 detects that it's talking
932-
to a gateway cache that supports ESI, it generates an ESI include tag. But
933-
if there is no gateway cache or if it does not support ESI, Symfony2 will
934-
just merge the included page content within the main one as it would have
935-
done were standalone set to ``false``.
908+
When using the default ``render`` function (or setting the strategy to
909+
``default``), Symfony2 merges the included page content within the main one
910+
before sending the response to the client. But when using ``esi`` strategy,
911+
*and* if Symfony2 detects that it's talking to a gateway cache that supports
912+
ESI, it generates an ESI include tag. But if there is no gateway cache or if
913+
it does not support ESI, Symfony2 will just merge the included page content
914+
within the main one as it would have done if you had used ``render``.
936915

937916
.. note::
938917

@@ -947,14 +926,52 @@ of the master page.
947926
948927
public function newsAction($max)
949928
{
950-
// ...
929+
// ...
951930
952-
$response->setSharedMaxAge(60);
931+
$response->setSharedMaxAge(60);
953932
}
954933
955934
With ESI, the full page cache will be valid for 600 seconds, but the news
956935
component cache will only last for 60 seconds.
957936

937+
When using a controller reference, the ESI tag should reference the embedded
938+
action as an accessible URL so the gateway cache can fetch it independently of
939+
the rest of the page. Of course, an action can't be accessed via a URL unless
940+
it has a route that points to it. Symfony2 takes care of this via a generic
941+
route and controller. For the ESI include tag to work properly, you must
942+
define the ``_proxy`` route:
943+
944+
.. configuration-block::
945+
946+
.. code-block:: yaml
947+
948+
# app/config/routing.yml
949+
_proxy:
950+
resource: "@FrameworkBundle/Resources/config/routing/proxy.xml"
951+
prefix: /proxy
952+
953+
.. code-block:: xml
954+
955+
<!-- app/config/routing.xml -->
956+
<?xml version="1.0" encoding="UTF-8" ?>
957+
958+
<routes xmlns="http://symfony.com/schema/routing"
959+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
960+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
961+
962+
<import resource="@FrameworkBundle/Resources/config/routing/proxy.xml" prefix="/proxy" />
963+
</routes>
964+
965+
.. code-block:: php
966+
967+
// app/config/routing.php
968+
use Symfony\Component\Routing\RouteCollection;
969+
use Symfony\Component\Routing\Route;
970+
971+
$collection->addCollection($loader->import('@FrameworkBundle/Resources/config/routing/proxy.xml', '/proxy'));
972+
973+
return $collection;
974+
958975
One great advantage of this caching strategy is that you can make your
959976
application as dynamic as needed and at the same time, hit the application as
960977
little as possible.
@@ -967,7 +984,7 @@ little as possible.
967984
obey the ``max-age`` directive and cache the entire page. And you don't
968985
want that.
969986

970-
The ``render`` helper supports two other useful options:
987+
The ``render_esi`` helper supports two other useful options:
971988

972989
* ``alt``: used as the ``alt`` attribute on the ESI tag, which allows you
973990
to specify an alternative URL to be used if the ``src`` cannot be found;

book/templating.rst

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -623,43 +623,8 @@ The ``recentList`` template is perfectly straightforward:
623623
(e.g. ``/article/*slug*``). This is a bad practice. In the next section,
624624
you'll learn how to do this correctly.
625625

626-
Even though this controller will only be used internally, you'll need to
627-
create a route that points to the controller:
628-
629-
.. configuration-block::
630-
631-
.. code-block:: yaml
632-
633-
latest_articles:
634-
pattern: /articles/latest/{max}
635-
defaults: { _controller: AcmeArticleBundle:Article:recentArticles }
636-
637-
.. code-block:: xml
638-
639-
<?xml version="1.0" encoding="UTF-8" ?>
640-
641-
<routes xmlns="http://symfony.com/schema/routing"
642-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
643-
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
644-
645-
<route id="latest_articles" pattern="/articles/latest/{max}">
646-
<default key="_controller">AcmeArticleBundle:Article:recentArticles</default>
647-
</route>
648-
</routes>
649-
650-
.. code-block:: php
651-
652-
use Symfony\Component\Routing\RouteCollection;
653-
use Symfony\Component\Routing\Route;
654-
655-
$collection = new RouteCollection();
656-
$collection->add('latest_articles', new Route('/articles/latest/{max}', array(
657-
'_controller' => 'AcmeArticleBundle:Article:recentArticles',
658-
)));
659-
660-
return $collection;
661-
662-
To include the controller, you'll need to refer to it using an absolute url:
626+
To include the controller, you'll need to refer to it using the standard
627+
string syntax for controllers (i.e. **bundle**:**controller**:**action**):
663628

664629
.. configuration-block::
665630

@@ -669,7 +634,7 @@ To include the controller, you'll need to refer to it using an absolute url:
669634

670635
{# ... #}
671636
<div id="sidebar">
672-
{% render url('latest_articles', { 'max': 3 }) %}
637+
{{ render(controller('AcmeArticleBundle:Article:recentArticles', { 'max': 3 })) }}
673638
</div>
674639

675640
.. code-block:: html+php
@@ -679,12 +644,10 @@ To include the controller, you'll need to refer to it using an absolute url:
679644
<!-- ... -->
680645
<div id="sidebar">
681646
<?php echo $view['actions']->render(
682-
$view['router']->generate('latest_articles', array('max' => 3), true)
647+
new ControllerReference('AcmeArticleBundle:Article:recentArticles', array('max' => 3))
683648
) ?>
684649
</div>
685650

686-
.. include:: /book/_security-2012-6431.rst.inc
687-
688651
Whenever you find that you need a variable or a piece of information that
689652
you don't have access to in a template, consider rendering a controller.
690653
Controllers are fast to execute and promote good code organization and reuse.
@@ -703,13 +666,20 @@ Symfony2 uses the standard ``render`` helper to configure ``hinclude`` tags:
703666

704667
.. code-block:: jinja
705668
706-
{% render url('...'), {'standalone': 'js'} %}
669+
{{ render_hinclude(controller('...')) }}
670+
671+
{{ render_hinclude(url('...')) }}
707672
708673
.. code-block:: php
709674
675+
<?php echo $view['actions']->render(
676+
new ControllerReference('...'),
677+
array('strategy' => 'hinclude')
678+
) ?>
679+
710680
<?php echo $view['actions']->render(
711681
$view['router']->generate('...'),
712-
array('standalone' => 'js')
682+
array('strategy' => 'hinclude')
713683
) ?>
714684
715685
.. note::
@@ -756,18 +726,14 @@ any global default templates that is defined):
756726

757727
.. code-block:: jinja
758728
759-
{% render '...:news' with
760-
{},
761-
{'standalone': 'js', 'default': 'AcmeDemoBundle:Default:content.html.twig'}
762-
%}
729+
{{ render_hinclude(controller('...'), {'default': 'AcmeDemoBundle:Default:content.html.twig'}) }}
763730
764731
.. code-block:: php
765732
766733
<?php echo $view['actions']->render(
767-
'...:news',
768-
array(),
734+
new ControllerReference('...'),
769735
array(
770-
'standalone' => 'js',
736+
'strategy' => 'hinclude',
771737
'default' => 'AcmeDemoBundle:Default:content.html.twig',
772738
)
773739
) ?>
@@ -778,18 +744,14 @@ Or you can also specify a string to display as the default content:
778744

779745
.. code-block:: jinja
780746
781-
{% render '...:news' with
782-
{},
783-
{'standalone': 'js', 'default': 'Loading...'}
784-
%}
747+
{{ render_hinclude(controller('...'), {'default': 'Loading...'}) }}
785748
786749
.. code-block:: php
787750
788751
<?php echo $view['actions']->render(
789-
'...:news',
790-
array(),
752+
new ControllerReference('...'),
791753
array(
792-
'standalone' => 'js',
754+
'strategy' => 'hinclude',
793755
'default' => 'Loading...',
794756
)
795757
) ?>

cookbook/service_container/scopes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ when compiling the container. Read the sidebar below for more details.
4848
*RequestA*) is passed to it. Life is good!
4949

5050
* You've now made a subrequest in Symfony, which is a fancy way of saying
51-
that you've called, for example, the `{% render ... %}` Twig function,
51+
that you've called, for example, the `{{ render(...) }}` Twig function,
5252
which executes another controller. Internally, the old `request` service
5353
(*RequestA*) is actually replaced by a new request instance (*RequestB*).
5454
This happens in the background, and it's totally normal.

cookbook/templating/render_without_controller.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ this is probably only useful if you'd like to cache this page partial (see
6464

6565
.. code-block:: html+jinja
6666

67-
{% render url('acme_privacy') %}
67+
{{ render(url('acme_privacy')) }}
6868

6969
.. code-block:: html+php
7070

quick_tour/the_view.rst

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -180,57 +180,19 @@ And what if you want to embed the result of another controller in a template?
180180
That's very useful when working with Ajax, or when the embedded template needs
181181
some variable not available in the main template.
182182

183-
Suppose you've created a ``fancyAction`` controller method, and you want to "render"
184-
it inside the ``index`` template. First, create a route to your new controller
185-
in one of your application's routing configuration files.
186-
187-
.. configuration-block::
188-
189-
.. code-block:: yaml
190-
191-
# app/config/routing.yml
192-
fancy:
193-
pattern: /included/fancy/{name}/{color}
194-
defaults: { _controller: AcmeDemoBundle:Demo:fancy }
195-
196-
.. code-block:: xml
197-
198-
<!-- app/config/routing.xml -->
199-
<?xml version="1.0" encoding="UTF-8" ?>
200-
<routes xmlns="http://symfony.com/schema/routing"
201-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
202-
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
203-
204-
<route id="fancy" pattern="/included/fancy/{name}/{color}">
205-
<default key="_controller">AcmeDemoBundle:Demo:fancy</default>
206-
</route>
207-
</routes>
208-
209-
.. code-block:: php
210-
211-
// app/config/routing.php
212-
use Symfony\Component\Routing\RouteCollection;
213-
use Symfony\Component\Routing\Route;
214-
215-
$collection = new RouteCollection();
216-
$collection->add('fancy', new Route('/included/fancy/{name}/{color}', array(
217-
'_controller' => 'AcmeDemoBundle:Demo:fancy',
218-
)));
219-
220-
return $collection;
221-
222-
To include the result (e.g. ``HTML``) of the controller, use the ``render`` tag:
183+
Suppose you've created a ``fancyAction`` controller method, and you want to
184+
"render" it inside the ``index`` template, which means including the result
185+
(e.g. ``HTML``) of the controller, use the ``render`` function:
223186

224187
.. code-block:: jinja
225188
226189
{# src/Acme/DemoBundle/Resources/views/Demo/index.html.twig #}
227-
{% render url('fancy', { 'name': name, 'color': 'green'}) %}
228-
229-
.. include:: /book/_security-2012-6431.rst.inc
190+
{{ render(controller("AcmeDemoBundle:Demo:fancy", {'name': name, 'color': 'green'})) }}
230191
231-
The ``render`` tag will execute the ``AcmeDemoBundle:Demo:fancy`` controller
232-
and include its result. For example, your new ``fancyAction`` might look
233-
like this::
192+
Here, the ``AcmeDemoBundle:Demo:fancy`` string refers to the ``fancy`` action
193+
of the ``Demo`` controller. The arguments (``name`` and ``color``) act like
194+
simulated request variables (as if the ``fancyAction`` were handling a whole
195+
new request) and are made available to the controller::
234196

235197
// src/Acme/DemoBundle/Controller/DemoController.php
236198

@@ -243,7 +205,7 @@ like this::
243205

244206
return $this->render('AcmeDemoBundle:Demo:fancy.html.twig', array(
245207
'name' => $name,
246-
'object' => $object
208+
'object' => $object,
247209
));
248210
}
249211

0 commit comments

Comments
 (0)