Skip to content

Commit 05e3332

Browse files
bug symfony#23459 [TwigBundle] allow to configure custom formats in XML configs (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [TwigBundle] allow to configure custom formats in XML configs | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#13554 | License | MIT | Doc PR | Commits ------- 5a3a24b allow to configure custom formats in XML configs
2 parents 3c9958c + 5a3a24b commit 05e3332

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
<xsd:complexType name="config">
1111
<xsd:sequence>
12+
<xsd:element name="date" type="date" minOccurs="0" maxOccurs="1" />
13+
<xsd:element name="number-format" type="number_format" minOccurs="0" maxOccurs="1" />
14+
1215
<!-- @deprecated since version 2.6, to be removed in 3.0 -->
1316
<xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
1417
<xsd:element name="form-theme" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@@ -28,6 +31,18 @@
2831
<xsd:attribute name="exception-controller" type="xsd:string" />
2932
</xsd:complexType>
3033

34+
<xsd:complexType name="date">
35+
<xsd:attribute name="format" type="xsd:string" />
36+
<xsd:attribute name="interval-format" type="xsd:string" />
37+
<xsd:attribute name="timezone" type="xsd:string" />
38+
</xsd:complexType>
39+
40+
<xsd:complexType name="number_format">
41+
<xsd:attribute name="decimals" type="xsd:integer" />
42+
<xsd:attribute name="decimal-point" type="xsd:string" />
43+
<xsd:attribute name="thousands-separator" type="xsd:string" />
44+
</xsd:complexType>
45+
3146
<xsd:complexType name="form">
3247
<xsd:choice minOccurs="1" maxOccurs="unbounded">
3348
<xsd:element name="resource" type="xsd:string" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$container->loadFromExtension('twig', array(
4+
'date' => array(
5+
'format' => 'Y-m-d',
6+
'interval_format' => '%d',
7+
'timezone' => 'Europe/Berlin',
8+
),
9+
'number_format' => array(
10+
'decimals' => 2,
11+
'decimal_point' => ',',
12+
'thousands_separator' => '.',
13+
),
14+
));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:twig="http://symfony.com/schema/dic/twig"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
7+
8+
<twig:config>
9+
<twig:date format="Y-m-d" interval-format="%d" timezone="Europe/Berlin" />
10+
<twig:number-format decimals="2" decimal-point="," thousands-separator="." />
11+
</twig:config>
12+
</container>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
twig:
2+
date:
3+
format: Y-m-d
4+
interval_format: '%d'
5+
timezone: Europe/Berlin
6+
number_format:
7+
decimals: 2
8+
decimal_point: ','
9+
thousands_separator: .

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
150150
$this->assertEquals('name', $options['autoescape']);
151151
}
152152

153+
/**
154+
* @dataProvider getFormats
155+
*/
156+
public function testLoadCustomDateFormats($fileFormat)
157+
{
158+
$container = $this->createContainer();
159+
$container->registerExtension(new TwigExtension());
160+
$this->loadFromFile($container, 'formats', $fileFormat);
161+
$this->compileContainer($container);
162+
163+
$environmentConfigurator = $container->getDefinition('twig.configurator.environment');
164+
165+
$this->assertSame('Y-m-d', $environmentConfigurator->getArgument(0));
166+
$this->assertSame('%d', $environmentConfigurator->getArgument(1));
167+
$this->assertSame('Europe/Berlin', $environmentConfigurator->getArgument(2));
168+
$this->assertSame(2, $environmentConfigurator->getArgument(3));
169+
$this->assertSame(',', $environmentConfigurator->getArgument(4));
170+
$this->assertSame('.', $environmentConfigurator->getArgument(5));
171+
}
172+
153173
public function testGlobalsWithDifferentTypesAndValues()
154174
{
155175
$globals = array(

0 commit comments

Comments
 (0)