4
4
How to Work with Different Output Formats in Templates
5
5
======================================================
6
6
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
8
8
most cases you'll use templates to render HTML content, a template can just
9
9
as easily generate JavaScript, CSS, XML or any other format you can dream of.
10
10
11
11
For example, the same "resource" is often rendered in several formats.
12
12
To render an article index page in XML, simply include the format in the
13
13
template name:
14
14
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 ``
17
17
18
18
In reality, this is nothing more than a naming convention and the template
19
19
isn't actually rendered differently based on its format.
@@ -22,36 +22,71 @@ In many cases, you may want to allow a single controller to render multiple
22
22
different formats based on the "request format". For that reason, a common
23
23
pattern is to do the following::
24
24
25
- public function indexAction(Request $request)
25
+ // ...
26
+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
+
28
+ class ArticleController extends Controller
26
29
{
27
- $format = $request->getRequestFormat();
30
+ /**
31
+ * @Route("/{slug}")
32
+ */
33
+ public function showAction(Request $request, $slug)
34
+ {
35
+ // retrieve the article based on $slug
36
+ $article = ...;
37
+
38
+ $format = $request->getRequestFormat();
28
39
29
- return $this->render('article/index.'.$format.'.twig');
40
+ return $this->render('article/show.'.$format.'.twig', array(
41
+ 'article' => $article,
42
+ ));
43
+ }
30
44
}
31
45
32
46
The ``getRequestFormat() `` on the ``Request `` object defaults to ``html ``,
33
47
but can return any other format based on the format requested by the user.
34
48
The request format is most often managed by the routing, where a route can
35
- be configured so that ``/contact `` sets the request format to ``html `` while
36
- ``/contact.xml `` sets the format to ``xml ``. For more information, see this
37
- :ref: `Advanced Routing Example <advanced-routing-example >`.
49
+ be configured so that ``/about-us `` sets the request format to ``html `` while
50
+ ``/about-us.xml `` sets the format to ``xml ``. This can be achieved by using the
51
+ special ``_format `` placeholder in your route definition::
52
+
53
+ /**
54
+ * @Route("/{slug}.{_format}", defaults={"_format": "html"})
55
+ */
56
+ public function showAction(Request $request, $slug)
57
+ {
58
+ // ...
59
+ }
38
60
39
- To create links that include the format parameter, include a ``_format ``
40
- key in the parameter hash :
61
+ Now, include the ``_format `` placeholder when generating a route for another
62
+ format :
41
63
42
64
.. configuration-block ::
43
65
44
66
.. code-block :: html+twig
45
67
46
- <a href="{{ path('article_show', {'id ': 123 , '_format': 'pdf '}) }}">
47
- PDF Version
68
+ <a href="{{ path('article_show', {'slug ': 'about-us' , '_format': 'xml '}) }}">
69
+ View as XML
48
70
</a>
49
71
50
72
.. code-block :: html+php
51
73
52
74
<a href="<?php echo $view['router']->generate('article_show', array(
53
- 'id ' => 123 ,
54
- '_format' => 'pdf ',
75
+ 'slug ' => 'about-us' ,
76
+ '_format' => 'xml ',
55
77
)) ?>">
56
- PDF Version
78
+ View as XML
57
79
</a>
80
+
81
+ .. seealso ::
82
+
83
+ For more information, see this :ref: `Advanced Routing Example <advanced-routing-example >`.
84
+
85
+ .. tip ::
86
+
87
+ When building APIs, using file name extensions often isn't the best
88
+ solution. The FOSRestBundle provides a request listener that uses content
89
+ negotiation. For more information, check out the bundle's `Request Format Listener `_
90
+ documentation.
91
+
92
+ .. _Request Format Listener : http://symfony.com/doc/current/bundles/FOSRestBundle/3-listener-support.html#format-listener
0 commit comments