.. index:: single: Templating Creating and Using Templates ============================ As explained in :doc:`the previous article `, controllers are responsible for handling each request that comes into a Symfony application and they usually end up rendering a template to generate the response contents. In reality, the controller delegates most of the heavy work to other places so that code can be tested and reused. When a controller needs to generate HTML, CSS or any other content, it hands the work off to the templating engine. In this article, you'll learn how to write powerful templates that can be used to return content to the user, populate email bodies, and more. You'll learn shortcuts, clever ways to extend templates and how to reuse template code. .. index:: single: Templating; What is a template? Templates --------- A template is simply a text file that can generate any text-based format (HTML, XML, CSV, LaTeX ...). The most familiar type of template is a *PHP* template - a text file parsed by PHP that contains a mix of text and PHP code: .. code-block:: html+php
{{ entry.body }}
{% endfor %} {% endblock %} .. note:: The parent template is stored in ``app/Resources/views/``, so its path is simply ``base.html.twig``. The template naming conventions are explained fully in :ref:`template-naming-locations`. The key to template inheritance is the ``{% extends %}`` tag. This tells the templating engine to first evaluate the base template, which sets up the layout and defines several blocks. The child template is then rendered, at which point the ``title`` and ``body`` blocks of the parent are replaced by those from the child. Depending on the value of ``blog_entries``, the output might look like this: .. code-block:: htmlThe body of the first post.
The body of the second post.
{{ article.body }}
Including this template from any other template is simple: .. code-block:: html+twig {# app/Resources/views/article/list.html.twig #} {% extends 'layout.html.twig' %} {% block body %}