Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
99a4fb35
Commit
99a4fb35
authored
Apr 30, 2017
by
Pascal Luna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added parameters to configure the Twig core extension behavior
parent
0a3b2a9d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
1 deletion
+63
-1
doc/providers/twig.rst
doc/providers/twig.rst
+23
-1
src/Silex/Provider/TwigServiceProvider.php
src/Silex/Provider/TwigServiceProvider.php
+18
-0
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
+22
-0
No files found.
doc/providers/twig.rst
View file @
99a4fb35
...
@@ -24,6 +24,28 @@ Parameters
...
@@ -24,6 +24,28 @@ Parameters
``bootstrap_3_layout.html.twig``, and
``bootstrap_3_layout.html.twig``, and
``bootstrap_3_horizontal_layout.html.twig``.
``bootstrap_3_horizontal_layout.html.twig``.
* **twig.date.format** (optional): Default format used by the ``date``
filter. The format string must conform to the format accepted by
`date() <http://www.php.net/date>`_.
* **twig.date.interval_format** (optional): Default format used by the
``date`` filter when the filtered data is of type `DateInterval <http://www.php.net/DateInterval>`_.
The format string must conform to the format accepted by
`DateInterval::format() <http://www.php.net/DateInterval.format>`_.
* **twig.date.timezone** (optional): Default timezone used when formatting
dates. If set to ``null`` the timezone returned by `date_default_timezone_get() <http://www.php.net/date_default_timezone_get>`_
is used.
* **twig.number_format.decimals** (optional): Default number of decimals
displayed by the ``number_format`` filter.
* **twig.number_format.decimal_point** (optional): Default separator for
the decimal point used by the ``number_format`` filter.
* **twig.number_format.thousands_separator** (optional): Default thousands
separator used by the ``number_format`` filter.
Services
Services
--------
--------
...
...
src/Silex/Provider/TwigServiceProvider.php
View file @
99a4fb35
...
@@ -41,6 +41,14 @@ class TwigServiceProvider implements ServiceProviderInterface
...
@@ -41,6 +41,14 @@ class TwigServiceProvider implements ServiceProviderInterface
$app
[
'twig.path'
]
=
array
();
$app
[
'twig.path'
]
=
array
();
$app
[
'twig.templates'
]
=
array
();
$app
[
'twig.templates'
]
=
array
();
$app
[
'twig.date.format'
]
=
'F j, Y H:i'
;
$app
[
'twig.date.interval_format'
]
=
'%d days'
;
$app
[
'twig.date.timezone'
]
=
null
;
$app
[
'twig.number_format.decimals'
]
=
0
;
$app
[
'twig.number_format.decimal_point'
]
=
'.'
;
$app
[
'twig.number_format.thousands_separator'
]
=
','
;
$app
[
'twig'
]
=
function
(
$app
)
{
$app
[
'twig'
]
=
function
(
$app
)
{
$app
[
'twig.options'
]
=
array_replace
(
$app
[
'twig.options'
]
=
array_replace
(
array
(
array
(
...
@@ -55,6 +63,16 @@ class TwigServiceProvider implements ServiceProviderInterface
...
@@ -55,6 +63,16 @@ class TwigServiceProvider implements ServiceProviderInterface
// deprecated and should probably be removed in Silex 3.0
// deprecated and should probably be removed in Silex 3.0
$twig
->
addGlobal
(
'app'
,
$app
);
$twig
->
addGlobal
(
'app'
,
$app
);
$coreExtension
=
$twig
->
getExtension
(
'Twig_Extension_Core'
);
$coreExtension
->
setDateFormat
(
$app
[
'twig.date.format'
],
$app
[
'twig.date.interval_format'
]);
if
(
null
!==
$app
[
'twig.date.timezone'
])
{
$coreExtension
->
setTimezone
(
$app
[
'twig.date.timezone'
]);
}
$coreExtension
->
setNumberFormat
(
$app
[
'twig.number_format.decimals'
],
$app
[
'twig.number_format.decimal_point'
],
$app
[
'twig.number_format.thousands_separator'
]);
if
(
$app
[
'debug'
])
{
if
(
$app
[
'debug'
])
{
$twig
->
addExtension
(
new
\Twig_Extension_Debug
());
$twig
->
addExtension
(
new
\Twig_Extension_Debug
());
}
}
...
...
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
View file @
99a4fb35
...
@@ -116,4 +116,26 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -116,4 +116,26 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
$this
->
assertInstanceOf
(
'Twig_Environment'
,
$app
[
'twig'
]);
$this
->
assertInstanceOf
(
'Twig_Environment'
,
$app
[
'twig'
]);
}
}
public
function
testFormatParameters
()
{
$app
=
new
Application
();
$timezone
=
new
\DateTimeZone
(
'Europe/Paris'
);
$app
->
register
(
new
TwigServiceProvider
(),
array
(
'twig.date.format'
=>
'Y-m-d'
,
'twig.date.interval_format'
=>
'%h hours'
,
'twig.date.timezone'
=>
$timezone
,
'twig.number_format.decimals'
=>
2
,
'twig.number_format.decimal_point'
=>
','
,
'twig.number_format.thousands_separator'
=>
' '
,
));
$twig
=
$app
[
'twig'
];
$this
->
assertSame
(
array
(
'Y-m-d'
,
'%h hours'
),
$twig
->
getExtension
(
'Twig_Extension_Core'
)
->
getDateFormat
());
$this
->
assertSame
(
$timezone
,
$twig
->
getExtension
(
'Twig_Extension_Core'
)
->
getTimezone
());
$this
->
assertSame
(
array
(
2
,
','
,
' '
),
$twig
->
getExtension
(
'Twig_Extension_Core'
)
->
getNumberFormat
());
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment