44How 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
88most cases you'll use templates to render HTML content, a template can just
99as easily generate JavaScript, CSS, XML or any other format you can dream of.
1010
1111For example, the same "resource" is often rendered in several formats.
1212To render an article index page in XML, simply include the format in the
1313template 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
1818In reality, this is nothing more than a naming convention and the template
1919isn'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
2222different formats based on the "request format". For that reason, a common
2323pattern is to do the following::
2424
25- public function indexAction(Request $request)
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, $slug)
34+ {
35+ // retrieve the article based on $slug
36+ $article = ...;
37+
38+ $format = $request->getRequestFormat();
2839
29- return $this->render('article/index.'.$format.'.twig');
40+ return $this->render('article/show.'.$format.'.twig', array(
41+ 'article' => $article,
42+ ));
43+ }
3044 }
3145
3246The ``getRequestFormat() `` on the ``Request `` object defaults to ``html ``,
3347but can return any other format based on the format requested by the user.
3448The 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+ }
3860
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 :
4163
4264.. configuration-block ::
4365
4466 .. code-block :: html+twig
4567
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
4870 </a>
4971
5072 .. code-block :: html+php
5173
5274 <a href="<?php echo $view['router']->generate('article_show', array(
53- 'id ' => 123 ,
54- '_format' => 'pdf ',
75+ 'slug ' => 'about-us' ,
76+ '_format' => 'xml ',
5577 )) ?>">
56- PDF Version
78+ View as XML
5779 </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