@@ -5,8 +5,8 @@ Twig Configuration Reference (TwigBundle)
5
5
=========================================
6
6
7
7
The TwigBundle integrates the Twig library in Symfony applications to
8
- :doc : `render templates </templating >`. All these options are configured under
9
- the ``twig `` key in your application configuration.
8
+ :ref : `render templates <templates-rendering >`. All these options are configured
9
+ under the ``twig `` key in your application configuration.
10
10
11
11
.. code-block :: terminal
12
12
@@ -41,8 +41,10 @@ Configuration
41
41
* `timezone `_
42
42
43
43
* `debug `_
44
+ * `default_path `_
44
45
* `exception_controller `_
45
46
* `form_themes `_
47
+ * `globals `_
46
48
* `number_format `_
47
49
48
50
* `decimals `_
@@ -62,6 +64,8 @@ If ``true``, whenever a template is rendered, Symfony checks first if its source
62
64
code has changed since it was compiled. If it has changed, the template is
63
65
compiled again automatically.
64
66
67
+ .. _config-twig-autoescape :
68
+
65
69
autoescape
66
70
~~~~~~~~~~
67
71
@@ -73,7 +77,7 @@ individually in the templates).
73
77
.. caution ::
74
78
75
79
Setting this option to ``false `` is dangerous and it will make your
76
- application vulnerable to XSS exploits because most third-party bundles
80
+ application vulnerable to ` XSS attacks `_ because most third-party bundles
77
81
assume that auto-escaping is enabled and they don't escape contents
78
82
themselves.
79
83
@@ -139,8 +143,8 @@ charset
139
143
**type **: ``string `` **default **: ``'%kernel.charset%' ``
140
144
141
145
The charset used by the template files. By default it's the same as the value of
142
- the `` kernel.charset `` container parameter, which is `` UTF-8 `` by default in
143
- Symfony applications.
146
+ the :ref: ` kernel.charset container parameter < configuration-kernel-charset >`,
147
+ which is `` UTF-8 `` by default in Symfony applications.
144
148
145
149
date
146
150
~~~~
@@ -181,6 +185,17 @@ debug
181
185
If ``true ``, the compiled templates include a ``__toString() `` method that can
182
186
be used to display their nodes.
183
187
188
+ .. _config-twig-default-path :
189
+
190
+ default_path
191
+ ~~~~~~~~~~~~
192
+
193
+ **type **: ``string `` **default **: ``'%kernel.project_dir%/templates' ``
194
+
195
+ The path to the directory where Symfony will look for the application Twig
196
+ templates by default. If you store the templates in more than one directory, use
197
+ the :ref: `paths <config-twig-paths >` option too.
198
+
184
199
.. _config-twig-exception-controller :
185
200
186
201
exception_controller
@@ -195,7 +210,8 @@ is what's responsible for rendering specific templates under different error
195
210
conditions (see :doc: `/controller/error_pages `). Modifying this
196
211
option is advanced. If you need to customize an error page you should use
197
212
the previous link. If you need to perform some behavior on an exception,
198
- you should add a listener to the ``kernel.exception `` event (see :ref: `dic-tags-kernel-event-listener `).
213
+ you should add an :doc: `event listener </event_dispatcher >` to the
214
+ :ref: `kernel.exception event <kernel-kernel.exception >`.
199
215
200
216
.. _config-twig-form-themes :
201
217
@@ -253,6 +269,14 @@ These global themes are applied to all forms, even those which use the
253
269
:ref: `form_theme Twig tag <reference-twig-tag-form-theme >`, but you can
254
270
:ref: `disable global themes for specific forms <disabling-global-themes-for-single-forms >`.
255
271
272
+ globals
273
+ ~~~~~~~
274
+
275
+ **type **: ``array `` **default **: ``[] ``
276
+
277
+ It defines the global variables injected automatically into all Twig templates.
278
+ Learn more about :doc: `Twig global variables </templating/global_variables >`.
279
+
256
280
number_format
257
281
~~~~~~~~~~~~~
258
282
@@ -301,32 +325,15 @@ on. Set it to ``0`` to disable all the optimizations. You can even enable or
301
325
disable these optimizations selectively, as explained in the Twig documentation
302
326
about `the optimizer extension `_.
303
327
304
- .. _config-twig-default-path :
305
-
306
- default_path
307
- ~~~~~~~~~~~~
308
-
309
- **type **: ``string `` **default **: ``'%kernel.project_dir%/templates' ``
310
-
311
- The default directory where Symfony will look for Twig templates.
312
-
313
328
.. _config-twig-paths :
314
329
315
330
paths
316
331
~~~~~
317
332
318
333
**type **: ``array `` **default **: ``null ``
319
334
320
- This option defines the directories where Symfony will look for Twig templates
321
- in addition to the default locations. Symfony looks for the templates in the
322
- following order:
323
-
324
- #. The directories defined in this option;
325
- #. The ``Resources/views/ `` directories of the bundles used in the application;
326
- #. The directory defined in the ``default_path `` option.
327
-
328
- The values of the ``paths `` option are defined as ``key: value `` pairs where the
329
- ``value `` part can be ``null ``. For example:
335
+ Defines the directories where application templates are stored in addition to
336
+ the directory defined in the :ref: `default_path option <config-twig-default-path >`:
330
337
331
338
.. configuration-block ::
332
339
@@ -336,7 +343,8 @@ The values of the ``paths`` option are defined as ``key: value`` pairs where the
336
343
twig :
337
344
# ...
338
345
paths :
339
- ' %kernel.project_dir%/vendor/acme/foo-bar/templates ' : ~
346
+ ' email/default/templates ' : ~
347
+ ' backend/templates ' : ' admin'
340
348
341
349
.. code-block :: xml
342
350
@@ -350,7 +358,8 @@ The values of the ``paths`` option are defined as ``key: value`` pairs where the
350
358
351
359
<twig : config >
352
360
<!-- ... -->
353
- <twig : path >%kernel.project_dir%/vendor/acme/foo-bar/templates</twig : path >
361
+ <twig : path >email/default/templates</twig : path >
362
+ <twig : path namespace =" admin" >backend/templates</twig : path >
354
363
</twig : config >
355
364
</container >
356
365
@@ -360,60 +369,12 @@ The values of the ``paths`` option are defined as ``key: value`` pairs where the
360
369
$container->loadFromExtension('twig', [
361
370
// ...
362
371
'paths' => [
363
- '%kernel.project_dir%/vendor/acme/foo-bar/templates' => null,
372
+ 'email/default/templates' => null,
373
+ 'backend/templates' => 'admin',
364
374
],
365
375
]);
366
376
367
- The directories defined in the ``paths `` option have more priority than the
368
- default directories defined by Symfony. In the above example, if the template
369
- exists in the ``acme/foo-bar/templates/ `` directory inside your application's
370
- ``vendor/ ``, it will be used by Symfony.
371
-
372
- If you provide a value for any path, Symfony will consider it the Twig namespace
373
- for that directory:
374
-
375
- .. configuration-block ::
376
-
377
- .. code-block :: yaml
378
-
379
- # config/packages/twig.yaml
380
- twig :
381
- # ...
382
- paths :
383
- ' %kernel.project_dir%/vendor/acme/foo-bar/templates ' : ' foo_bar'
384
-
385
- .. code-block :: xml
386
-
387
- <!-- config/packages/twig.xml -->
388
- <container xmlns =" http://symfony.com/schema/dic/services"
389
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
390
- xmlns : twig =" http://symfony.com/schema/dic/twig"
391
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
392
- https://symfony.com/schema/dic/services/services-1.0.xsd
393
- http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd" >
394
-
395
- <twig : config >
396
- <!-- ... -->
397
- <twig : path namespace =" foo_bar" >%kernel.project_dir%/vendor/acme/foo-bar/templates</twig : path >
398
- </twig : config >
399
- </container >
400
-
401
- .. code-block :: php
402
-
403
- # config/packages/twig.php
404
- $container->loadFromExtension('twig', [
405
- // ...
406
- 'paths' => [
407
- '%kernel.project_dir%/vendor/acme/foo-bar/templates' => 'foo_bar',
408
- ],
409
- ]);
410
-
411
- This option is useful to not mess with the default template directories defined
412
- by Symfony. Besides, it simplifies how you refer to those templates:
413
-
414
- .. code-block :: text
415
-
416
- @foo_bar/template_name.html.twig
377
+ Read more about :ref: `template directories and namespaces <templates-namespaces >`.
417
378
418
379
strict_variables
419
380
~~~~~~~~~~~~~~~~
@@ -425,3 +386,4 @@ attribute or method doesn't exist. If set to ``false`` these errors are ignored
425
386
and the non-existing values are replaced by ``null ``.
426
387
427
388
.. _`the optimizer extension` : https://twig.symfony.com/doc/2.x/api.html#optimizer-extension
389
+ .. _`XSS attacks` : https://en.wikipedia.org/wiki/Cross-site_scripting
0 commit comments