From: Fabien Potencier Date: Sun, 18 Dec 2011 10:12:38 +0000 (+0100) Subject: added a way to change the default format for the date filter X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=f8b7441e42814cee6abc52b41c4ab4c86f2a6b6b;p=web%2Fkonrad%2Ftwig.git added a way to change the default format for the date filter --- diff --git a/CHANGELOG b/CHANGELOG index 76b62a1..ee559ee 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.5.0 + * added a way to change the default format for the date filter * fixed the lexer when an operator ending with a letter ends a line * added string interpolation support * enhanced exceptions for unknown filters, functions, tests, and tags diff --git a/doc/filters/date.rst b/doc/filters/date.rst index 32032ac..897645e 100644 --- a/doc/filters/date.rst +++ b/doc/filters/date.rst @@ -4,6 +4,9 @@ .. versionadded:: 1.1 The timezone support has been added in Twig 1.1. +.. versionadded:: 1.5 + The default date format support has been added in Twig 1.5. + The ``date`` filter formats a date to a given format: .. code-block:: jinja @@ -30,5 +33,13 @@ You can also specify a timezone: {{ post.published_at|date("m/d/Y", "Europe/Paris") }} +If no format is provided, Twig will use the default one: ``F j, Y H:i``. This +default can be easily changed: + +.. code-block:: php + + $twig = new Twig_Environment($loader); + $twig->getExtension('core')->setDateFormat('d/m/Y'); + .. _`date`: http://www.php.net/date .. _`DateTime`: http://www.php.net/manual/en/datetime.construct.php diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 325f56d..42846e4 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -14,6 +14,28 @@ if (!defined('ENT_SUBSTITUTE')) { */ class Twig_Extension_Core extends Twig_Extension { + protected $dateFormat = 'F j, Y H:i'; + + /** + * Sets the default format to be used by the date filter. + * + * @param string $format The default date format string + */ + public function setDateFormat($format) + { + $this->dateFormat = $format; + } + + /** + * Gets the default format to be used by the date filter. + * + * @return string The default date format string + */ + public function getDateFormat() + { + return $this->dateFormat; + } + /** * Returns the token parser instance to add to the existing list. * @@ -46,7 +68,7 @@ class Twig_Extension_Core extends Twig_Extension { $filters = array( // formatting filters - 'date' => new Twig_Filter_Function('twig_date_format_filter'), + 'date' => new Twig_Filter_Function('twig_date_format_filter', array('needs_environment' => true)), 'format' => new Twig_Filter_Function('sprintf'), 'replace' => new Twig_Filter_Function('strtr'), @@ -228,14 +250,19 @@ function twig_cycle($values, $i) * {{ post.published_at|date("m/d/Y") }} * * + * @param Twig_Environment $env A Twig_Environment instance * @param DateTime|string $date A date * @param string $format A format * @param DateTimeZone|string $timezone A timezone * * @return string The formatter date */ -function twig_date_format_filter($date, $format = 'F j, Y H:i', $timezone = null) +function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null) { + if (null === $format) { + $format = $env->getExtension('core')->getDateFormat(); + } + if (!$date instanceof DateTime && !$date instanceof DateInterval) { $asString = (string) $date; if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { diff --git a/test/Twig/Tests/Fixtures/filters/date_default_format.test b/test/Twig/Tests/Fixtures/filters/date_default_format.test new file mode 100644 index 0000000..5e1d8de --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/date_default_format.test @@ -0,0 +1,14 @@ +--TEST-- +"date" filter +--TEMPLATE-- +{{ date|date }} +{{ date|date('d/m/Y') }} +--DATA-- +date_default_timezone_set('UTC'); +$twig->getExtension('core')->setDateFormat('Y-m-d'); +return array( + 'date' => mktime(13, 45, 0, 10, 4, 2010), +) +--EXPECT-- +2010-10-04 +04/10/2010