Navigation Menu

Skip to content

Commit

Permalink
simplified the implementation of the replace filter
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 27, 2011
1 parent 86214c4 commit fba7f95
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
19 changes: 1 addition & 18 deletions lib/Twig/Extension/Core.php
Expand Up @@ -48,7 +48,7 @@ public function getFilters()
// 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'),
Expand Down Expand Up @@ -458,23 +458,6 @@ function twig_in_filter($value, $compare)
return false;
}

/**
* Replaces placeholders in a string.
*
* <pre>
* {{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }}
* </pre>
*
* @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.
*
Expand Down
8 changes: 8 additions & 0 deletions 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.

0 comments on commit fba7f95

Please sign in to comment.