Skip to content

Commit 3858249

Browse files
committed
Used the article example consistently in the example
1 parent 5862c75 commit 3858249

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

templating/formats.rst

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
How to Work with Different Output Formats in Templates
55
======================================================
66

7-
Templates are a generic way to render content in *any* format. And while in
7+
Templates are a generic way to render content in *any* format. While in
88
most cases you'll use templates to render HTML content, a template can just
99
as easily generate JavaScript, CSS, XML or any other format you can dream of.
1010

1111
For example, the same "resource" is often rendered in several formats.
1212
To render an article index page in XML, simply include the format in the
1313
template name:
1414

15-
* *XML template name*: ``article/index.xml.twig``
16-
* *XML template filename*: ``index.xml.twig``
15+
* *XML template name*: ``article/show.xml.twig``
16+
* *XML template filename*: ``show.xml.twig``
1717

1818
In reality, this is nothing more than a naming convention and the template
1919
isn't actually rendered differently based on its format.
@@ -22,38 +22,59 @@ In many cases, you may want to allow a single controller to render multiple
2222
different formats based on the "request format". For that reason, a common
2323
pattern is to do the following::
2424

25-
public function showAction(Request $request, Article $entity)
25+
// ...
26+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27+
28+
class ArticleController extends Controller
2629
{
27-
$format = $request->getRequestFormat();
30+
/**
31+
* @Route("/{slug}")
32+
*/
33+
public function showAction(Request $request, Article $article)
34+
{
35+
$format = $request->getRequestFormat();
2836

29-
return $this->render('article/index.'.$format.'.twig', [
30-
'entity' => $entity
31-
]);
37+
return $this->render('article/show.'.$format.'.twig', array(
38+
'article' => $article
39+
));
40+
}
3241
}
3342

3443
The ``getRequestFormat()`` on the ``Request`` object defaults to ``html``,
3544
but can return any other format based on the format requested by the user.
3645
The request format is most often managed by the routing, where a route can
37-
be configured so that ``/contact`` sets the request format to ``html`` while
38-
``/contact.xml`` sets the format to ``xml``. For more information, see this
39-
:ref:`Advanced Routing Example <advanced-routing-example>`.
46+
be configured so that ``/about-us`` sets the request format to ``html`` while
47+
``/about-us.xml`` sets the format to ``xml``. This can be achieved by using the
48+
special ``_format`` placeholder in your route definition::
49+
50+
/**
51+
* @Route("/{slug}.{_format}", defaults={"_format": "html"})
52+
*/
53+
public function showAction(Request $request, Article $article)
54+
{
55+
// ...
56+
}
4057

41-
To create links that include the format parameter, include a ``_format``
42-
key in the parameter hash:
58+
Now, include the ``_format`` placeholder when generating a route for another
59+
format:
4360

4461
.. configuration-block::
4562

4663
.. code-block:: html+twig
4764

48-
<a href="{{ path('article_show', {'id': 123, '_format': 'pdf'}) }}">
49-
PDF Version
65+
<a href="{{ path('article_show', {'slug': 'about-us', '_format': 'xml'}) }}">
66+
View as XML
5067
</a>
5168

5269
.. code-block:: html+php
5370

5471
<a href="<?php echo $view['router']->generate('article_show', array(
55-
'id' => 123,
56-
'_format' => 'pdf',
72+
'slug' => 'about-us',
73+
'_format' => 'xml',
5774
)) ?>">
58-
PDF Version
75+
View as XML
5976
</a>
77+
78+
.. seealso::
79+
80+
For more information, see this :ref:`Advanced Routing Example <advanced-routing-example>`.

0 commit comments

Comments
 (0)