From e9bc23b54af8ad3df06b2a1093d168f9d70554b3 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 30 Jan 2015 09:44:56 +0100 Subject: [PATCH] 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: . ``` --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 1 + .../DependencyInjection/Configuration.php | 30 ++++++++++++ .../Configurator/EnvironmentConfigurator.php | 48 +++++++++++++++++++ .../DependencyInjection/TwigExtension.php | 8 ++++ .../TwigBundle/Resources/config/twig.xml | 10 ++++ 5 files changed, 97 insertions(+) create mode 100644 src/Symfony/Bundle/TwigBundle/DependencyInjection/Configurator/EnvironmentConfigurator.php diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 82325777bc..90f7cbd1e0 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 2.7.0 ----- + * made it possible to configure the default formats for both the `date` and the `number_format` filter * added support for the new Asset component (from Twig bridge) * deprecated the assets extension (use the one from the Twig bridge instead) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php index 7aaa48d476..794144eb5d 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php @@ -42,6 +42,7 @@ class Configuration implements ConfigurationInterface $this->addFormThemesSection($rootNode); $this->addGlobalsSection($rootNode); $this->addTwigOptions($rootNode); + $this->addTwigFormatOptions($rootNode); return $treeBuilder; } @@ -207,4 +208,33 @@ class Configuration implements ConfigurationInterface ->end() ; } + + private function addTwigFormatOptions(ArrayNodeDefinition $rootNode) + { + $rootNode + ->children() + ->arrayNode('date') + ->info('The default format options used by the date filter') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('format')->defaultValue('F j, Y H:i')->end() + ->scalarNode('interval_format')->defaultValue('%d days')->end() + ->scalarNode('timezone') + ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used') + ->defaultNull() + ->end() + ->end() + ->end() + ->arrayNode('number_format') + ->info('The default format options for the number_format filter') + ->addDefaultsIfNotSet() + ->children() + ->integerNode('decimals')->defaultValue(0)->end() + ->scalarNode('decimal_point')->defaultValue('.')->end() + ->scalarNode('thousands_separator')->defaultValue(',')->end() + ->end() + ->end() + ->end() + ; + } } diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configurator/EnvironmentConfigurator.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configurator/EnvironmentConfigurator.php new file mode 100644 index 0000000000..21e9a1a25c --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configurator/EnvironmentConfigurator.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\TwigBundle\DependencyInjection\Configurator; + +/** + * Twig environment configurator. + * + * @author Christian Flothmann + */ +class EnvironmentConfigurator +{ + private $dateFormat; + private $intervalFormat; + private $timezone; + private $decimals; + private $decimalPoint; + private $thousandsSeparator; + + public function __construct($dateFormat, $intervalFormat, $timezone, $decimals, $decimalPoint, $thousandsSeparator) + { + $this->dateFormat = $dateFormat; + $this->intervalFormat = $intervalFormat; + $this->timezone = $timezone; + $this->decimals = $decimals; + $this->decimalPoint = $decimalPoint; + $this->thousandsSeparator = $thousandsSeparator; + } + + public function configure(\Twig_Environment $environment) + { + $environment->getExtension('core')->setDateFormat($this->dateFormat, $this->intervalFormat); + + if (null !== $this->timezone) { + $environment->getExtension('core')->setTimezone($this->timezone); + } + + $environment->getExtension('core')->setNumberFormat($this->decimals, $this->decimalPoint, $this->thousandsSeparator); + } +} diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index 70ed9eed9c..8b28042bd4 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -57,6 +57,14 @@ class TwigExtension extends Extension $container->setParameter('twig.form.resources', $config['form_themes']); + $envConfiguratorDefinition = $container->getDefinition('twig.configurator.environment'); + $envConfiguratorDefinition->replaceArgument(0, $config['date']['format']); + $envConfiguratorDefinition->replaceArgument(1, $config['date']['interval_format']); + $envConfiguratorDefinition->replaceArgument(2, $config['date']['timezone']); + $envConfiguratorDefinition->replaceArgument(3, $config['number_format']['decimals']); + $envConfiguratorDefinition->replaceArgument(4, $config['number_format']['decimal_point']); + $envConfiguratorDefinition->replaceArgument(5, $config['number_format']['thousands_separator']); + $twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem'); // register user-configured paths diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 5c84d25059..9e1a117774 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -35,6 +35,7 @@ app + @@ -163,5 +164,14 @@ %twig.exception_listener.controller% + + + + + + + + +