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,38 +22,62 @@ 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 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, $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', [
30- 'entity' => $entity
31- ]);
40+ return $this->render('article/show.'.$format.'.twig', array(
41+ 'article' => $article,
42+ ));
43+ }
3244 }
3345
3446The ``getRequestFormat() `` on the ``Request `` object defaults to ``html ``,
3547but can return any other format based on the format requested by the user.
3648The 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 >`.
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::
4052
41- To create links that include the format parameter, include a ``_format ``
42- key in the parameter hash:
53+ /**
54+ * @Route("/{slug}.{_format}", defaults={"_format": "html"})
55+ */
56+ public function showAction(Request $request, $slug)
57+ {
58+ // ...
59+ }
60+
61+ Now, include the ``_format `` placeholder when generating a route for another
62+ format:
4363
4464.. configuration-block ::
4565
4666 .. code-block :: html+twig
4767
48- <a href="{{ path('article_show', {'id ': 123 , '_format': 'pdf '}) }}">
49- PDF Version
68+ <a href="{{ path('article_show', {'slug ': 'about-us' , '_format': 'xml '}) }}">
69+ View as XML
5070 </a>
5171
5272 .. code-block :: html+php
5373
5474 <a href="<?php echo $view['router']->generate('article_show', array(
55- 'id ' => 123 ,
56- '_format' => 'pdf ',
75+ 'slug ' => 'about-us' ,
76+ '_format' => 'xml ',
5777 )) ?>">
58- PDF Version
78+ View as XML
5979 </a>
80+
81+ .. seealso ::
82+
83+ For more information, see this :ref: `Advanced Routing Example <advanced-routing-example >`.
0 commit comments