Skip to content

Commit c7716eb

Browse files
committed
make date formats and number formats configurable
This adds new Twig configuration options that make it possible to configure the format of both numbers and dates as well as timezones without the need to write custom code. For example, using the new configuration options can look like this: ```yaml twig: date: format: d.m.Y, H:i:s interval_format: %%d days timezone: Europe/Berlin number_format: decimals: 2 decimal_point: , thousands_separator: . ```
1 parent 78b1a68 commit c7716eb

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function getConfigTreeBuilder()
4242
$this->addFormThemesSection($rootNode);
4343
$this->addGlobalsSection($rootNode);
4444
$this->addTwigOptions($rootNode);
45+
$this->addTwigFormatOptions($rootNode);
4546

4647
return $treeBuilder;
4748
}
@@ -207,4 +208,33 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
207208
->end()
208209
;
209210
}
211+
212+
private function addTwigFormatOptions(ArrayNodeDefinition $rootNode)
213+
{
214+
$rootNode
215+
->children()
216+
->arrayNode('date')
217+
->info('The default format options used by the date filter')
218+
->addDefaultsIfNotSet()
219+
->children()
220+
->scalarNode('format')->defaultNull()->end()
221+
->scalarNode('interval_format')->defaultNull()->end()
222+
->scalarNode('timezone')
223+
->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
224+
->defaultNull()
225+
->end()
226+
->end()
227+
->end()
228+
->arrayNode('number_format')
229+
->info('The default format options for the number_format filter')
230+
->addDefaultsIfNotSet()
231+
->children()
232+
->integerNode('decimals')->defaultValue(0)->end()
233+
->scalarNode('decimal_point')->defaultValue('.')->end()
234+
->scalarNode('thousands_separator')->defaultValue(',')->end()
235+
->end()
236+
->end()
237+
->end()
238+
;
239+
}
210240
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Configurator;
13+
14+
/**
15+
* Twig environment configurator.
16+
*
17+
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
18+
*/
19+
class EnvironmentConfigurator
20+
{
21+
private $dateFormat;
22+
private $intervalFormat;
23+
private $timezone;
24+
private $decimals;
25+
private $decimalPoint;
26+
private $thousandsSeparator;
27+
28+
public function __construct($dateFormat, $intervalFormat, $timezone, $decimals, $decimalPoint, $thousandsSeparator)
29+
{
30+
$this->dateFormat = $dateFormat;
31+
$this->intervalFormat = $intervalFormat;
32+
$this->timezone = $timezone;
33+
$this->decimals = $decimals;
34+
$this->decimalPoint = $decimalPoint;
35+
$this->thousandsSeparator = $thousandsSeparator;
36+
}
37+
38+
public function configure(\Twig_Environment $environment)
39+
{
40+
$environment->getExtension('core')->setDateFormat($this->dateFormat, $this->intervalFormat);
41+
42+
if (null !== $this->timezone) {
43+
$environment->getExtension('core')->setTimezone($this->timezone);
44+
}
45+
46+
$environment->getExtension('core')->setNumberFormat($this->decimals, $this->decimalPoint, $this->thousandsSeparator);
47+
}
48+
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public function load(array $configs, ContainerBuilder $container)
5757

5858
$container->setParameter('twig.form.resources', $config['form_themes']);
5959

60+
$container->setParameter('twig.date.format', $config['date']['format']);
61+
$container->setParameter('twig.date.interval_format', $config['date']['interval_format']);
62+
$container->setParameter('twig.date.timezone', $config['date']['timezone']);
63+
$container->setParameter('twig.number_format.decimal_point', $config['number_format']['decimal_point']);
64+
$container->setParameter('twig.number_format.decimals', $config['number_format']['decimals']);
65+
$container->setParameter('twig.number_format.thousands_separator', $config['number_format']['thousands_separator']);
66+
6067
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');
6168

6269
// register user-configured paths

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<argument>app</argument>
3636
<argument type="service" id="twig.app_variable" />
3737
</call>
38+
<configurator service="twig.configurator.environment" method="configure" />
3839
</service>
3940

4041
<service id="twig.app_variable" class="Symfony\Bridge\Twig\AppVariable" public="false">
@@ -163,5 +164,14 @@
163164
<argument type="service" id="http_kernel" />
164165
<argument>%twig.exception_listener.controller%</argument>
165166
</service>
167+
168+
<service id="twig.configurator.environment" class="Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator" public="false">
169+
<argument>%twig.date.format%</argument>
170+
<argument>%twig.date.interval_format%</argument>
171+
<argument>%twig.date.timezone%</argument>
172+
<argument>%twig.number_format.decimals%</argument>
173+
<argument>%twig.number_format.decimal_point%</argument>
174+
<argument>%twig.number_format.thousands_separator%</argument>
175+
</service>
166176
</services>
167177
</container>

0 commit comments

Comments
 (0)