9898
9999 {{ title|upper }}
100100
101- Twig comes with a long list of `tags `_ and `filters `_ that are available
102- by default. You can even `add your own extensions `_ to Twig as needed.
101+ Twig comes with a long list of `tags `_, `filters `_ and `functions `_ that are available
102+ by default. You can even add your own *custom * filters, functions (and more) via
103+ a :doc: `Twig Extension </templating/twig_extension >`.
103104
104- .. tip ::
105-
106- Registering a Twig extension is as easy as creating a new service and tagging
107- it with ``twig.extension `` :ref: `tag <reference-dic-tags-twig-extension >`.
108-
109- As you'll see throughout the documentation, Twig also supports functions
110- and new functions can be easily added. For example, the following uses a
111- standard ``for `` tag and the ``cycle `` function to print ten div tags, with
112- alternating ``odd ``, ``even `` classes:
105+ Twig code will look similar to PHP code, with subtle, nice differences. The following
106+ example uses a standard ``for `` tag and the ``cycle `` function to print ten div tags,
107+ with alternating ``odd ``, ``even `` classes:
113108
114109.. code-block :: html+twig
115110
@@ -121,11 +116,6 @@ alternating ``odd``, ``even`` classes:
121116
122117Throughout this chapter, template examples will be shown in both Twig and PHP.
123118
124- .. tip ::
125-
126- If you *do * choose to not use Twig and you disable it, you'll need to implement
127- your own exception handler via the ``kernel.exception `` event.
128-
129119.. sidebar :: Why Twig?
130120
131121 Twig templates are meant to be simple and won't process PHP tags. This
@@ -157,22 +147,11 @@ Throughout this chapter, template examples will be shown in both Twig and PHP.
157147Twig Template Caching
158148~~~~~~~~~~~~~~~~~~~~~
159149
160- Twig is fast. Each Twig template is compiled down to a native PHP class
161- that is rendered at runtime. The compiled classes are located in the
162- ``app/cache/{environment}/twig `` directory (where ``{environment} `` is the
163- environment, such as ``dev `` or ``prod ``) and in some cases can be useful
164- while debugging. See :ref: `environments-summary ` for more information on
165- environments.
166-
167- When ``debug `` mode is enabled (common in the ``dev `` environment), a Twig
168- template will be automatically recompiled when changes are made to it. This
169- means that during development you can happily make changes to a Twig template
170- and instantly see the changes without needing to worry about clearing any
171- cache.
172-
173- When ``debug `` mode is disabled (common in the ``prod `` environment), however,
174- you must clear the Twig cache directory so that the Twig templates will
175- regenerate. Remember to do this when deploying your application.
150+ Twig is fast because each template is compiled to a native PHP class and cached.
151+ But don't worry: this happens automatically and doesn't require *you * to do anything.
152+ And while you're developing, Twig is smart enough to re-compile you templates after
153+ you make any changes. That means Twig is fast in production, but easy to use while
154+ developing.
176155
177156.. index ::
178157 single: Templating; Inheritance
@@ -336,9 +315,10 @@ Notice that since the child template didn't define a ``sidebar`` block, the
336315value from the parent template is used instead. Content within a ``{% block %} ``
337316tag in a parent template is always used by default.
338317
339- You can use as many levels of inheritance as you want. In the next section,
340- a common three-level inheritance model will be explained along with how templates
341- are organized inside a Symfony project.
318+ .. tip ::
319+
320+ You can use as many levels of inheritance as you want! See :doc: `/templating/inheritance `
321+ for more info.
342322
343323When working with template inheritance, here are some tips to keep in mind:
344324
@@ -387,7 +367,7 @@ By default, templates can live in two different locations:
387367 well as templates that override third party bundle templates
388368 (see :doc: `/templating/overriding `).
389369
390- ``path/to/bundle /Resources/views/ ``
370+ ``vendor/ path/to/CoolBundle /Resources/views/ ``
391371 Each third party bundle houses its templates in its ``Resources/views/ ``
392372 directory (and subdirectories). When you plan to share your bundle, you should
393373 put the templates in the bundle instead of the ``app/ `` directory.
@@ -404,9 +384,9 @@ to render/extend ``app/Resources/views/base.html.twig``, you'll use the
404384Referencing Templates in a Bundle
405385~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
406386
407- Symfony uses a **bundle **:**directory **:**filename ** string syntax for
408- templates that live inside a bundle . This allows for several types of
409- templates, each which lives in a specific location:
387+ * If * you need to refer to a template that lives in a bundle, Symfony uses a **bundle **:**directory **:**filename **
388+ string syntax . This allows for several types of templates, each which lives in a
389+ specific location:
410390
411391* ``AcmeBlogBundle:Blog:index.html.twig ``: This syntax is used to specify a
412392 template for a specific page. The three parts of the string, each separated
@@ -935,30 +915,35 @@ is by default "web").
935915The end result is a page that includes both the ``main.css `` and ``contact.css ``
936916stylesheets.
937917
938- Final Thoughts
939- --------------
918+ Referencing the Request, User or Session
919+ ----------------------------------------
920+
921+ Symfony also gives you a global ``app `` variable in Twig that can be used to access
922+ the current user, the Request and more.
923+
924+ See :doc: `/templating/app_variable ` for details.
925+
926+ Output Escaping
927+ ---------------
928+
929+ Twig performs automatic "output escaping" when rendering any content in order to
930+ protect you from Cross Site Scripting (XSS) attacks.
931+
932+ Suppose ``description `` equals ``I <3 this product ``:
933+
934+ .. code-block :: twig
940935
941- The templating engine in Symfony is a powerful tool that can be used each time
942- you need to generate presentational content in HTML, XML or any other format.
943- And though templates are a common way to generate content in a controller,
944- their use is not mandatory. The ``Response `` object returned by a controller
945- can be created with or without the use of a template::
936+ <!-- outupt escaping is on automatically -->
937+ {{ description }} <!-- I <3 this product -->
946938
947- // creates a Response object whose content is the rendered template
948- $response = $ this->render('article/index.html.twig');
939+ <!-- disable output escaping with the raw filter -->
940+ {{ description|raw }} <!-- I <3 this product -->
949941
950- // creates a Response object whose content is simple text
951- $response = new Response('response content');
942+ .. caution ::
952943
953- Symfony's templating engine is very flexible and two different template
954- renderers are available by default: the traditional *PHP * templates and the
955- sleek and powerful *Twig * templates. Both support a template hierarchy and
956- come packaged with a rich set of helper functions capable of performing
957- the most common tasks.
944+ PHP templates do not automatically escape content.
958945
959- Overall, the topic of templating should be thought of as a powerful tool
960- that's at your disposal. In some cases, you may not need to render a template,
961- and in Symfony, that's absolutely fine.
946+ For more details, see :doc: `/templating/escaping `.
962947
963948Learn more
964949----------
@@ -972,6 +957,7 @@ Learn more
972957.. _`Twig` : http://twig.sensiolabs.org
973958.. _`tags` : http://twig.sensiolabs.org/doc/tags/index.html
974959.. _`filters` : http://twig.sensiolabs.org/doc/filters/index.html
960+ .. _`functions` : http://twig.sensiolabs.org/doc/functions/index.html
975961.. _`add your own extensions` : http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension
976962.. _`hinclude.js` : http://mnot.github.io/hinclude/
977963.. _`with_context` : http://twig.sensiolabs.org/doc/functions/include.html
0 commit comments