Skip to content

Commit 16ce462

Browse files
committed
minor #15274 [Twig] [ConfigBuilder] Use ConfigBuilder for TwigBundle (Nyholm)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Twig] [ConfigBuilder] Use ConfigBuilder for TwigBundle Here are config builder for TwigBundle Commits ------- 28d3a56 [Twig] [ConfigBuilder] Use ConfigBuilder for TwigBundle
2 parents 5c37580 + 28d3a56 commit 16ce462

File tree

9 files changed

+81
-58
lines changed

9 files changed

+81
-58
lines changed

configuration/front_controllers_and_kernel.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,13 @@ parameter used, for example, to turn Twig's debug mode on:
190190
191191
.. code-block:: php
192192
193-
$container->loadFromExtension('twig', [
194-
'debug' => '%kernel.debug%',
193+
// config/packages/twig.php
194+
use Symfony\Config\TwigConfig;
195+
196+
return static function (TwigConfig $twig) {
195197
// ...
196-
]);
198+
$twig->debug('%kernel.debug%');
199+
};
197200
198201
The Environments
199202
----------------

configuration/override_dir_structure.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ for multiple directories):
140140
.. code-block:: php
141141
142142
// config/packages/twig.php
143-
$container->loadFromExtension('twig', [
144-
'default_path' => '%kernel.project_dir%/resources/views',
145-
]);
143+
use Symfony\Config\TwigConfig;
144+
145+
return static function (TwigConfig $twig) {
146+
$twig->defaultPath('%kernel.project_dir%/resources/views');
147+
};
146148
147149
Override the Translations Directory
148150
-----------------------------------

form/bootstrap4.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ configuration:
5555
.. code-block:: php
5656
5757
// config/packages/twig.php
58-
$container->loadFromExtension('twig', [
59-
'form_themes' => [
60-
'bootstrap_4_layout.html.twig',
61-
],
58+
use Symfony\Config\TwigConfig;
59+
60+
return static function (TwigConfig $twig) {
61+
$twig->formThemes(['bootstrap_4_layout.html.twig']);
6262
6363
// ...
64-
]);
64+
};
6565
6666
If you prefer to apply the Bootstrap styles on a form to form basis, include the
6767
``form_theme`` tag in the templates where those forms are used:

form/create_custom_field_type.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,14 @@ rest of files):
405405
.. code-block:: php
406406
407407
// config/packages/twig.php
408-
$container->loadFromExtension('twig', [
409-
'form_themes' => [
408+
use Symfony\Config\TwigConfig;
409+
410+
return static function (TwigConfig $twig) {
411+
$twig->formThemes([
410412
'form/custom_types.html.twig',
411413
'...',
412-
],
413-
]);
414+
]);
415+
};
414416
415417
The last step is to create the actual Twig template that will render the type.
416418
The template contents depend on which HTML, CSS and JavaScript frameworks and

form/form_themes.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,15 @@ want to use another theme for all the forms of your app, configure it in the
8888
.. code-block:: php
8989
9090
// config/packages/twig.php
91-
$container->loadFromExtension('twig', [
92-
'form_themes' => [
91+
use Symfony\Config\TwigConfig;
92+
93+
return static function (TwigConfig $twig) {
94+
$twig->formThemes([
9395
'bootstrap_4_horizontal_layout.html.twig',
94-
],
96+
]);
97+
9598
// ...
96-
]);
99+
};
97100
98101
You can pass multiple themes to this option because sometimes form themes only
99102
redefine a few elements. This way, if some theme doesn't override some element,
@@ -514,12 +517,15 @@ you want to apply the theme globally to all forms, define the
514517
.. code-block:: php
515518
516519
// config/packages/twig.php
517-
$container->loadFromExtension('twig', [
518-
'form_themes' => [
520+
use Symfony\Config\TwigConfig;
521+
522+
return static function (TwigConfig $twig) {
523+
$twig->formThemes([
519524
'form/my_theme.html.twig',
520-
],
525+
]);
526+
521527
// ...
522-
]);
528+
};
523529
524530
If you only want to apply it to some specific forms, use the ``form_theme`` tag:
525531

mailer.rst

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,14 @@ image files as usual. First, to simplify things, define a Twig namespace called
704704
.. code-block:: php
705705
706706
// config/packages/twig.php
707-
$container->loadFromExtension('twig', [
707+
use Symfony\Config\TwigConfig;
708+
709+
return static function (TwigConfig $twig) {
708710
// ...
709-
'paths' => [
710-
// point this wherever your images live
711-
'%kernel.project_dir%/assets/images' => 'images',
712-
],
713-
]);
711+
712+
// point this wherever your images live
713+
$twig->path('%kernel.project_dir%/assets/images', 'images');
714+
};
714715
715716
Now, use the special ``email.image()`` Twig helper to embed the images inside
716717
the email contents:
@@ -812,13 +813,14 @@ called ``styles`` that points to the directory where ``email.css`` lives:
812813
.. code-block:: php
813814
814815
// config/packages/twig.php
815-
$container->loadFromExtension('twig', [
816+
use Symfony\Config\TwigConfig;
817+
818+
return static function (TwigConfig $twig) {
816819
// ...
817-
'paths' => [
818-
// point this wherever your css files live
819-
'%kernel.project_dir%/assets/styles' => 'styles',
820-
],
821-
]);
820+
821+
// point this wherever your css files live
822+
$twig->path('%kernel.project_dir%/assets/styles', 'styles');
823+
};
822824
823825
.. _mailer-markdown:
824826

reference/configuration/twig.rst

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,16 @@ all the forms of the application:
235235
.. code-block:: php
236236
237237
// config/packages/twig.php
238-
$container->loadFromExtension('twig', [
239-
'form_themes' => [
238+
use Symfony\Config\TwigConfig;
239+
240+
return static function (TwigConfig $twig) {
241+
$twig->formThemes([
240242
'bootstrap_4_layout.html.twig',
241243
'form/my_theme.html.twig',
242-
],
244+
]);
245+
243246
// ...
244-
]);
247+
};
245248
246249
The order in which themes are defined is important because each theme overrides
247250
all the previous one. When rendering a form field whose block is not defined in
@@ -348,13 +351,14 @@ the directory defined in the :ref:`default_path option <config-twig-default-path
348351
.. code-block:: php
349352
350353
// config/packages/twig.php
351-
$container->loadFromExtension('twig', [
354+
use Symfony\Config\TwigConfig;
355+
356+
return static function (TwigConfig $twig) {
352357
// ...
353-
'paths' => [
354-
'email/default/templates' => null,
355-
'backend/templates' => 'admin',
356-
],
357-
]);
358+
359+
$twig->path('email/default/templates', null);
360+
$twig->path('backend/templates', 'admin');
361+
};
358362
359363
Read more about :ref:`template directories and namespaces <templates-namespaces>`.
360364

templating/global_variables.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ main Twig configuration file:
3939
.. code-block:: php
4040
4141
// config/packages/twig.php
42-
$container->loadFromExtension('twig', [
42+
use Symfony\Config\TwigConfig;
43+
44+
return static function (TwigConfig $twig) {
4345
// ...
44-
'globals' => [
45-
'ga_tracking' => 'UA-xxxxx-x',
46-
],
47-
]);
46+
47+
$twig->global('ga_tracking')->value('UA-xxxxx-x');
48+
};
4849
4950
Now, the variable ``ga_tracking`` is available in all Twig templates, so you
5051
can use it without having to pass it explicitly from the controller or service
@@ -98,12 +99,13 @@ the ``@`` character, which is the usual syntax to
9899
.. code-block:: php
99100
100101
// config/packages/twig.php
101-
$container->loadFromExtension('twig', [
102+
use Symfony\Config\TwigConfig;
103+
104+
return static function (TwigConfig $twig) {
102105
// ...
103-
'globals' => [
104-
'uuid' => '@App\Generator\UuidGenerator',
105-
],
106-
]);
106+
107+
$twig->global('uuid')->value('@App\Generator\UuidGenerator');
108+
};
107109
108110
Now you can use the ``uuid`` variable in any Twig template to access to the
109111
``UuidGenerator`` service:

testing.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,11 @@ code to production:
184184
.. code-block:: php
185185
186186
// config/packages/test/twig.php
187-
$container->loadFromExtension('twig', [
188-
'strict_variables' => true,
189-
]);
187+
use Symfony\Config\TwigConfig;
188+
189+
return static function (TwigConfig $twig) {
190+
$twig->strictVariables(true);
191+
};
190192
191193
You can also use a different environment entirely, or override the default
192194
debug mode (``true``) by passing each as options to the ``bootKernel()``

0 commit comments

Comments
 (0)