From 1e9493df294705a61410b5e1a35ef20b082519ae Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 11 Sep 2010 16:41:50 -0400 Subject: [PATCH] added the constant() test (closes #120) --- CHANGELOG | 1 + doc/02-Twig-for-Template-Designers.markdown | 10 ++++++++++ lib/Twig/Extension/Core.php | 6 ++++++ test/Twig/Tests/Fixtures/tests/const.test | 12 ++++++++++++ test/Twig/Tests/integrationTest.php | 2 ++ 5 files changed, 31 insertions(+), 0 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tests/const.test diff --git a/CHANGELOG b/CHANGELOG index ae1e046..5f57cd7 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" test * fixed objects with __toString() not being autoescaped * fixed subscript expressions when calling __call() (methods now keep the case) * added a "trans" filter diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 1dd9d25..6f8c972 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -1239,6 +1239,16 @@ variable: the foo attribute really is the `false` PHP value {% endif %} +### `constant` + +`constant` checks if a variable has the exact same value as a constant. You +can use either global constants or class constants: + + [twig] + {% if post.status is constant('Post::PUBLISHED') %} + the status attribute is exactly the same as Post::PUBLISHED + {% endif %} + Extensions ---------- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 6934fb3..725816f 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -95,6 +95,7 @@ class Twig_Extension_Core extends Twig_Extension 'sameas' => new Twig_Test_Function('twig_test_sameas'), 'none' => new Twig_Test_Function('twig_test_none'), 'divisibleby' => new Twig_Test_Function('twig_test_divisibleby'), + 'constant' => new Twig_Test_Function('twig_test_constant'), ); } @@ -332,3 +333,8 @@ function twig_test_odd($value) { return $value % 2 == 1; } + +function twig_test_constant($value, $constant) +{ + return constant($constant) === $value; +} diff --git a/test/Twig/Tests/Fixtures/tests/const.test b/test/Twig/Tests/Fixtures/tests/const.test new file mode 100644 index 0000000..83d33f4 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tests/const.test @@ -0,0 +1,12 @@ +--TEST-- +"const" test +--TEMPLATE-- +{{ 30719 is constant('E_ALL') ? 'ok' : 'no' }} +{{ 'bar' is constant('Foo::BAR_NAME') ? 'ok' : 'no' }} +{{ value is constant('Foo::BAR_NAME') ? 'ok' : 'no' }} +--DATA-- +return array('value' => 'bar'); +--EXPECT-- +ok +ok +ok \ No newline at end of file diff --git a/test/Twig/Tests/integrationTest.php b/test/Twig/Tests/integrationTest.php index 9958d51..14ac53a 100644 --- a/test/Twig/Tests/integrationTest.php +++ b/test/Twig/Tests/integrationTest.php @@ -81,6 +81,8 @@ class Twig_Tests_IntegrationTest extends PHPUnit_Framework_TestCase class Foo { + const BAR_NAME = 'bar'; + public function bar($param1 = null, $param2 = null) { return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : ''); -- 1.7.2.5