diff --git a/templating.rst b/templating.rst index 833d7fea232..65b6d4c4dd3 100644 --- a/templating.rst +++ b/templating.rst @@ -6,74 +6,75 @@ Creating and Using Templates As you know, the :doc:`controller ` is responsible for handling each request that comes into a Symfony application. 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. +the controller delegates most of the heavy work to other places. When +a controller needs to generate HTML, it hands the work off to the +*templating* engine. + In this chapter, 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. +Rendering a Template +-------------------- + +Typically, you'll render a template from inside of your controller:: + + // src/AppBundle/Controller/LuckyController.php + public function numberAction() + { + $number = mt_rand(0, 10); + + // renders app/Resources/views/lucky/number.html.twig + return $this->render('lucky/number.html.twig', array( + // pass two variables into your template + 'randomNumber' => $number, + 'people' => array('Javier', 'Christian, 'Wouter') + )) + } + +This template passes two variables - ``randomNumber`` and ``people`` into your +template. A very simple template might look like this: + +.. code-block:: html+twig + + {# app/Resources/views/lucky/number.html.twig #} +
{{ entry.body }}
- {% endfor %} +getBody() ?>
- + stop() ?> -.. note:: - - The parent template is identified by a special string syntax - (``base.html.twig``). This path is relative to the ``app/Resources/views`` - directory of the project. You could also use the logical name equivalent: - ``::base.html.twig``. This naming convention is 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 +by those from the child. Depending on the value of ``randomNumber``, the output might look like this: .. code-block:: html - - - - -The body of the first post.
- -The body of the second post.
-