From fba7f95bd2637af9a75bd0f3c0efb2bbf227ee9f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 27 Nov 2011 14:58:28 +0100 Subject: [PATCH] simplified the implementation of the replace filter --- lib/Twig/Extension/Core.php | 19 +------------------ test/Twig/Tests/Fixtures/filters/replace.test | 8 ++++++++ 2 files changed, 9 insertions(+), 18 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/replace.test diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index f98ce5a..b79807f 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -48,7 +48,7 @@ class Twig_Extension_Core extends Twig_Extension // formatting filters 'date' => new Twig_Filter_Function('twig_date_format_filter'), 'format' => new Twig_Filter_Function('sprintf'), - 'replace' => new Twig_Filter_Function('twig_strtr'), + 'replace' => new Twig_Filter_Function('strtr'), // encoding 'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'), @@ -459,23 +459,6 @@ function twig_in_filter($value, $compare) } /** - * Replaces placeholders in a string. - * - *
- *  {{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }}
- * 
- * - * @param string $pattern A string - * @param string $replacements The values for the placeholders - * - * @return string The string where the placeholders have been replaced - */ -function twig_strtr($pattern, $replacements) -{ - return str_replace(array_keys($replacements), array_values($replacements), $pattern); -} - -/** * Escapes a string. * * @param Twig_Environment $env A Twig_Environment instance diff --git a/test/Twig/Tests/Fixtures/filters/replace.test b/test/Twig/Tests/Fixtures/filters/replace.test new file mode 100644 index 0000000..4021660 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/replace.test @@ -0,0 +1,8 @@ +--TEST-- +"replace" filter +--TEMPLATE-- +{{ "I like %this% and %that%."|replace({'%this%': "foo", '%that%': "bar"}) }} +--DATA-- +return array() +--EXPECT-- +I like foo and bar. -- 1.7.2.5