mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-11 09:56:32 +00:00
simplified the implementation of the replace filter
This commit is contained in:
@@ -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'),
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
Reference in New Issue
Block a user