Skip to content

Commit 01a02c6

Browse files
committed
Minor tweaks!
1 parent 3a6f662 commit 01a02c6

File tree

4 files changed

+53
-61
lines changed

4 files changed

+53
-61
lines changed

controller.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ creating unnecessarily deep structures::
483483
Templates are a generic way to render content in *any* format. And while in
484484
most cases you'll use templates to render HTML content, a template can just
485485
as easily generate JavaScript, CSS, XML or any other format you can dream of.
486-
To learn how to render different templating formats read the :ref:`/templating/formats`
486+
487+
To learn how to render different templating formats read the :doc:`/templating/formats`
487488
section of the Creating and Using Templates chapter.
488489

489490
The Symfony templating engine is explained in great detail in the

templating.rst

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,13 @@ it:
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

122117
Throughout 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.
157147
Twig 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
336315
value from the parent template is used instead. Content within a ``{% block %}``
337316
tag 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

343323
When 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
404384
Referencing 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").
935915
The end result is a page that includes both the ``main.css`` and ``contact.css``
936916
stylesheets.
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 &lt3 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

963948
Learn 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

templating/PHP.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ plain PHP code if you want. Both templating engines are supported equally in
99
Symfony. Symfony adds some nice features on top of PHP to make writing
1010
templates with PHP more powerful.
1111

12+
.. tip::
13+
14+
If you choose *not* use Twig and you disable it, you'll need to implement
15+
your own exception handler via the ``kernel.exception`` event.
16+
1217
Rendering PHP Templates
1318
-----------------------
1419

templating/app_variable.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. index::
22
single: Templating; app Variable
33

4-
How to Access Global Information through the ``app`` Variable
5-
=============================================================
4+
How to Access the User, Request, Session & more in Twig via the ``app`` Variable
5+
================================================================================
66

77
During each request, Symfony will set a global template variable ``app``
88
in both Twig and PHP template engines by default. The ``app`` variable

0 commit comments

Comments
 (0)