Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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: .
```
  • Loading branch information
xabbuh committed Mar 27, 2015
1 parent 78b1a68 commit 82b249b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Expand Up @@ -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)

Expand Down
Expand Up @@ -42,6 +42,7 @@ public function getConfigTreeBuilder()
$this->addFormThemesSection($rootNode);
$this->addGlobalsSection($rootNode);
$this->addTwigOptions($rootNode);
$this->addTwigFormatOptions($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -207,4 +208,33 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
->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()
;
}
}
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <christian.flothmann@xabbuh.de>
*/
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);
}
}
Expand Up @@ -57,6 +57,13 @@ public function load(array $configs, ContainerBuilder $container)

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

$container->setParameter('twig.date.format', $config['date']['format']);
$container->setParameter('twig.date.interval_format', $config['date']['interval_format']);
$container->setParameter('twig.date.timezone', $config['date']['timezone']);
$container->setParameter('twig.number_format.decimal_point', $config['number_format']['decimal_point']);
$container->setParameter('twig.number_format.decimals', $config['number_format']['decimals']);
$container->setParameter('twig.number_format.thousands_separator', $config['number_format']['thousands_separator']);

$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');

// register user-configured paths
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -35,6 +35,7 @@
<argument>app</argument>
<argument type="service" id="twig.app_variable" />
</call>
<configurator service="twig.configurator.environment" method="configure" />
</service>

<service id="twig.app_variable" class="Symfony\Bridge\Twig\AppVariable" public="false">
Expand Down Expand Up @@ -163,5 +164,14 @@
<argument type="service" id="http_kernel" />
<argument>%twig.exception_listener.controller%</argument>
</service>

<service id="twig.configurator.environment" class="Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator" public="false">
<argument>%twig.date.format%</argument>
<argument>%twig.date.interval_format%</argument>
<argument>%twig.date.timezone%</argument>
<argument>%twig.number_format.decimals%</argument>
<argument>%twig.number_format.decimal_point%</argument>
<argument>%twig.number_format.thousands_separator%</argument>
</service>
</services>
</container>

0 comments on commit 82b249b

Please sign in to comment.