Skip to content

Commit 8831d88

Browse files
committed
Merge branch '4.1'
* 4.1: Better explain render() and render_esi() functions Updated the embed controllers article Use <button> instead of <a> for JavaScript interactive buttons
2 parents 9f8f97f + e81246d commit 8831d88

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

form/form_collections.rst

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ will be show next):
353353
var $collectionHolder;
354354
355355
// setup an "add a tag" link
356-
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
357-
var $newLinkLi = $('<li></li>').append($addTagLink);
356+
var $addTagButton = $('<button type="button" class="add_tag_link">Add a tag</a>');
357+
var $newLinkLi = $('<li></li>').append($addTagButton);
358358
359359
jQuery(document).ready(function() {
360360
// Get the ul that holds the collection of tags
@@ -367,10 +367,7 @@ will be show next):
367367
// index when inserting a new item (e.g. 2)
368368
$collectionHolder.data('index', $collectionHolder.find(':input').length);
369369
370-
$addTagLink.on('click', function(e) {
371-
// prevent the link from creating a "#" on the URL
372-
e.preventDefault();
373-
370+
$addTagButton.on('click', function(e) {
374371
// add a new tag form (see next code block)
375372
addTagForm($collectionHolder, $newLinkLi);
376373
});
@@ -619,7 +616,7 @@ Template Modifications
619616

620617
The ``allow_delete`` option means that if an item of a collection
621618
isn't sent on submission, the related data is removed from the collection
622-
on the server. In order for this to work in an HTML form, you must remove
619+
on the server. In order for this to work in an HTML form, you must remove
623620
the DOM element for the collection item to be removed, before submitting
624621
the form.
625622

@@ -651,13 +648,10 @@ The ``addTagFormDeleteLink()`` function will look something like this:
651648
.. code-block:: javascript
652649
653650
function addTagFormDeleteLink($tagFormLi) {
654-
var $removeFormA = $('<a href="#">delete this tag</a>');
655-
$tagFormLi.append($removeFormA);
656-
657-
$removeFormA.on('click', function(e) {
658-
// prevent the link from creating a "#" on the URL
659-
e.preventDefault();
651+
var $removeFormButton = $('<button type="button">Delete this tag</a>');
652+
$tagFormLi.append($removeFormButton);
660653
654+
$removeFormButton.on('click', function(e) {
661655
// remove the li for the tag form
662656
$tagFormLi.remove();
663657
});

reference/forms/types/collection.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ you need is this JavaScript code:
168168
// add-collection-widget.js
169169
jQuery(document).ready(function () {
170170
jQuery('.add-another-collection-widget').click(function (e) {
171-
e.preventDefault();
172171
var list = jQuery(jQuery(this).attr('data-list'));
173172
// Try to find the counter of the list
174173
var counter = list.data('widget-counter') | list.children().length;
@@ -211,7 +210,7 @@ And update the template as follows:
211210
{% endfor %}
212211
</ul>
213212
214-
<a href="#"
213+
<button type="button"
215214
class="add-another-collection-widget"
216215
data-list="#email-fields-list">Add another email</a>
217216

reference/twig_reference.rst

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,21 @@ render
5353
``options`` *(optional)*
5454
**type**: ``array`` **default**: ``[]``
5555

56-
Renders the fragment for the given controller (using the `controller`_ function)
57-
or URI. For more information, see :doc:`/templating/embedding_controllers`.
56+
Makes a request to the given internal URI or controller and returns the result.
57+
It's commonly used to :doc:`embed controllers in templates </templating/embedding_controllers>`.
5858

59-
The render strategy can be specified in the ``strategy`` key of the options.
59+
.. code-block:: twig
6060
61-
.. tip::
61+
{# if the controller is associated with a route, use the path() or
62+
url() functions to generate the URI used by render() #}
63+
{{ render(path('latest_articles', {num: 5})) }}
64+
{{ render(url('latest_articles', {num: 5})) }}
65+
66+
{# if you don't want to expose the controller with a public URL, use
67+
the controller() function to define the controller to be executed #}
68+
{{ render(controller('App\\Controller\\DefaultController::latestArticles', {num: 5})) }}
6269
63-
The URI can be generated by other functions, like `path`_ and `url`_.
70+
The render strategy can be specified in the ``strategy`` key of the options.
6471

6572
.. _reference-twig-function-render-esi:
6673

@@ -76,13 +83,9 @@ render_esi
7683
``options`` *(optional)*
7784
**type**: ``array`` **default**: ``[]``
7885

79-
Generates an ESI tag when possible or falls back to the behavior of
80-
`render`_ function instead. For more information, see
81-
:doc:`/templating/embedding_controllers`.
82-
83-
.. tip::
84-
85-
The URI can be generated by other functions, like `path`_ and `url`_.
86+
It's similar to the `render`_ function and defines the same arguments. However,
87+
it generates an ESI tag when :doc:`ESI support </http_cache/esi>` is enabled or
88+
falls back to the behavior of `render`_ otherwise.
8689

8790
.. tip::
8891

templating/embedding_controllers.rst

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@
44
How to Embed Controllers in a Template
55
======================================
66

7-
In some cases, you need to do more than include a simple template. Suppose
8-
you have a sidebar in your layout that contains the three most recent articles.
9-
Retrieving the three articles may include querying the database or performing
10-
other heavy logic that can't be done from within a template.
11-
127
.. note::
138

149
Rendering embedded controllers is "heavier" than including a template or calling
1510
a custom Twig function. Unless you're planning on :doc:`caching the fragment </http_cache/esi>`,
1611
avoid embedding many controllers.
1712

18-
The solution is to simply embed the result of an entire controller from your
19-
template. First, create a controller that renders a certain number of recent
20-
articles::
13+
:ref:`Including template fragments <including-other-templates>` is useful to
14+
reuse the same content on several pages. However, this technique is not the best
15+
solution in some cases.
16+
17+
Consider a website that displays on its sidebar the most recently published
18+
articles. This list of articles is dynamic and it's probably the result of a
19+
database query. In other words, the controller of any page that displays that
20+
sidebar must make the same database query and pass the list of articles to the
21+
included template fragment.
22+
23+
The alternative solution proposed by Symfony is to create a controller that only
24+
displays the list of recent articles and then call to that controller from any
25+
template that needs to display that content.
26+
27+
First, create a controller that renders a certain number of recent articles::
2128

2229
// src/Controller/ArticleController.php
2330
namespace App\Controller;
@@ -39,15 +46,16 @@ articles::
3946
}
4047
}
4148

42-
The ``recent_list`` template is perfectly straightforward:
49+
Then, create a ``recent_list`` template fragment to list the articles given by
50+
the controller:
4351

4452
.. configuration-block::
4553

4654
.. code-block:: html+twig
4755

4856
{# templates/article/recent_list.html.twig #}
4957
{% for article in articles %}
50-
<a href="/article/{{ article.slug }}">
58+
<a href="{{ path('article_show', {slug: article.slug}) }}">
5159
{{ article.title }}
5260
</a>
5361
{% endfor %}
@@ -56,19 +64,15 @@ The ``recent_list`` template is perfectly straightforward:
5664

5765
<!-- templates/article/recent_list.html.php -->
5866
<?php foreach ($articles as $article): ?>
59-
<a href="/article/<?php echo $article->getSlug() ?>">
67+
<a href="<?php echo $view['router']->path('article_show', array(
68+
'slug' => $article->getSlug(),
69+
)) ?>">
6070
<?php echo $article->getTitle() ?>
6171
</a>
6272
<?php endforeach ?>
6373

64-
.. note::
65-
66-
Notice that the article URL is hardcoded in this example
67-
(e.g. ``/article/*slug*``). This is a bad practice. In the next section,
68-
you'll learn how to do this correctly.
69-
70-
To include the controller, you'll need to refer to it using the standard
71-
string syntax for controllers (i.e. **controllerNamespace**::**action**):
74+
Finally, call the controller from any template using the ``render()`` function
75+
and the standard string syntax for controllers (i.e. **controllerNamespace**::**action**):
7276

7377
.. configuration-block::
7478

@@ -97,5 +101,3 @@ string syntax for controllers (i.e. **controllerNamespace**::**action**):
97101
)
98102
) ?>
99103
</div>
100-
101-
The result of an embedded controller can also be :doc:`cached </http_cache/esi>`

0 commit comments

Comments
 (0)