From 1cf01ce9279c4e3c0a7b4a05117afc26c4434778 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 12 Sep 2010 07:59:41 +0200 Subject: [PATCH] added a constant filter (refs #120) --- CHANGELOG | 1 + doc/02-Twig-for-Template-Designers.markdown | 7 +++++++ lib/Twig/Extension/Core.php | 6 ++++++ test/Twig/Tests/Fixtures/filters/constant.test | 12 ++++++++++++ 4 files changed, 26 insertions(+), 0 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/constant.test diff --git a/CHANGELOG b/CHANGELOG index 5f57cd7..b783fd1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ Backward incompatibilities: * the odd and even filters are now tests: {{ foo|odd }} must now be written {{ foo is odd }} + * added a "constant" filter * added a "constant" test * fixed objects with __toString() not being autoescaped * fixed subscript expressions when calling __call() (methods now keep the case) diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 6f8c972..f93939b 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -1198,6 +1198,13 @@ with automatic escaping enabled this variable will not be escaped. {{ var|safe }} {# var won't be escaped #} {% autoescape off %} +### `constant` (new in Twig 0.9.9) + +`constant` returns the constant value for a given string: + + [twig] + {{ some_date|date('DATE_W3C'|constant) }} + List of built-in Tests (new in Twig 0.9.9) ------------------------------------------ diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 725816f..695aa9f 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -53,6 +53,7 @@ class Twig_Extension_Core extends Twig_Extension 'upper' => new Twig_Filter_Function('strtoupper'), 'lower' => new Twig_Filter_Function('strtolower'), 'striptags' => new Twig_Filter_Function('strip_tags'), + 'constant' => new Twig_Filter_Function('twig_constant_filter'), // array helpers 'join' => new Twig_Filter_Function('twig_join_filter'), @@ -198,6 +199,11 @@ function twig_cycle_filter($values, $i) return $values[$i % count($values)]; } +function twig_constant_filter($constant) +{ + return constant($constant); +} + /* * Each type specifies a way for applying a transformation to a string * The purpose is for the string to be "escaped" so it is suitable for diff --git a/test/Twig/Tests/Fixtures/filters/constant.test b/test/Twig/Tests/Fixtures/filters/constant.test new file mode 100644 index 0000000..d630038 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/constant.test @@ -0,0 +1,12 @@ +--TEST-- +"constant" filter +--TEMPLATE-- +{{ date|date('DATE_W3C'|constant) }} +--DATA-- +$d = date_default_timezone_get(); +date_default_timezone_set('UTC'); +$ret = array('date' => strtotime('2005-10-15 10:00:01')); +date_default_timezone_set($d); +return $ret; +--EXPECT-- +2005-10-15T10:00:01+00:00 -- 1.7.2.5