@@ -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,39 +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
- .. deprecated :: 4.2
321
-
322
- Using the ``src/Resources/views/ `` directory to store templates was
323
- deprecated in Symfony 4.2. Use instead the directory defined in the
324
- ``default_path `` option (which is ``templates/ `` by default).
325
-
326
- This option defines the directories where Symfony will look for Twig templates
327
- in addition to the default locations. Symfony looks for the templates in the
328
- following order:
329
-
330
- #. The directories defined in this option;
331
- #. The ``Resources/views/ `` directories of the bundles used in the application;
332
- #. The ``src/Resources/views/ `` directory of the application;
333
- #. The directory defined in the ``default_path `` option.
334
-
335
- The values of the ``paths `` option are defined as ``key: value `` pairs where the
336
- ``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 >`:
337
337
338
338
.. configuration-block ::
339
339
@@ -343,7 +343,8 @@ The values of the ``paths`` option are defined as ``key: value`` pairs where the
343
343
twig :
344
344
# ...
345
345
paths :
346
- ' %kernel.project_dir%/vendor/acme/foo-bar/templates ' : ~
346
+ ' email/default/templates ' : ~
347
+ ' backend/templates ' : ' admin'
347
348
348
349
.. code-block :: xml
349
350
@@ -357,7 +358,8 @@ The values of the ``paths`` option are defined as ``key: value`` pairs where the
357
358
358
359
<twig : config >
359
360
<!-- ... -->
360
- <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 >
361
363
</twig : config >
362
364
</container >
363
365
@@ -367,60 +369,12 @@ The values of the ``paths`` option are defined as ``key: value`` pairs where the
367
369
$container->loadFromExtension('twig', [
368
370
// ...
369
371
'paths' => [
370
- '%kernel.project_dir%/vendor/acme/foo-bar/templates' => null,
372
+ 'email/default/templates' => null,
373
+ 'backend/templates' => 'admin',
371
374
],
372
375
]);
373
376
374
- The directories defined in the ``paths `` option have more priority than the
375
- default directories defined by Symfony. In the above example, if the template
376
- exists in the ``acme/foo-bar/templates/ `` directory inside your application's
377
- ``vendor/ ``, it will be used by Symfony.
378
-
379
- If you provide a value for any path, Symfony will consider it the Twig namespace
380
- for that directory:
381
-
382
- .. configuration-block ::
383
-
384
- .. code-block :: yaml
385
-
386
- # config/packages/twig.yaml
387
- twig :
388
- # ...
389
- paths :
390
- ' %kernel.project_dir%/vendor/acme/foo-bar/templates ' : ' foo_bar'
391
-
392
- .. code-block :: xml
393
-
394
- <!-- config/packages/twig.xml -->
395
- <container xmlns =" http://symfony.com/schema/dic/services"
396
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
397
- xmlns : twig =" http://symfony.com/schema/dic/twig"
398
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
399
- https://symfony.com/schema/dic/services/services-1.0.xsd
400
- http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd" >
401
-
402
- <twig : config >
403
- <!-- ... -->
404
- <twig : path namespace =" foo_bar" >%kernel.project_dir%/vendor/acme/foo-bar/templates</twig : path >
405
- </twig : config >
406
- </container >
407
-
408
- .. code-block :: php
409
-
410
- # config/packages/twig.php
411
- $container->loadFromExtension('twig', [
412
- // ...
413
- 'paths' => [
414
- '%kernel.project_dir%/vendor/acme/foo-bar/templates' => 'foo_bar',
415
- ],
416
- ]);
417
-
418
- This option is useful to not mess with the default template directories defined
419
- by Symfony. Besides, it simplifies how you refer to those templates:
420
-
421
- .. code-block :: text
422
-
423
- @foo_bar/template_name.html.twig
377
+ Read more about :ref: `template directories and namespaces <templates-namespaces >`.
424
378
425
379
strict_variables
426
380
~~~~~~~~~~~~~~~~
@@ -432,3 +386,4 @@ attribute or method doesn't exist. If set to ``false`` these errors are ignored
432
386
and the non-existing values are replaced by ``null ``.
433
387
434
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