From: Fabien Potencier Date: Fri, 1 Oct 2010 10:08:46 +0000 (+0200) Subject: added a replace filter X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=e4d3f4366496352cb1caa0e18556d29f96bedfac;p=konrad%2Ftwig.git added a replace filter --- diff --git a/CHANGELOG b/CHANGELOG index d045514..c647692 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -8,11 +8,10 @@ Backward incompatibilities: * fixed error handling for if tag when a syntax error occurs within a subparse process * added a way to implement custom logic for resolving token parsers given a tag name * fixed js escaper to be stricter (now uses a whilelist-based js escaper) - * added a "constant" filter + * added the following filers: "constant", "trans", "replace" * 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 * added "test" feature (accessible via the "is" operator) * removed the debug tag (should be done in an extension) * fixed trans tag when no vars are used in plural form diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index c0dace8..d46c1d6 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -1023,14 +1023,24 @@ The `date` filter accepts any date format supported by ### `format` -The `format` filter formats a given string by replacing the placeholders: - +The `format` filter formats a given string by replacing the placeholders +(placeholders follows the `printf` notation): [twig] {# string is a format string like: I like %s and %s. #} {{ string|format(foo, "bar") }} {# returns I like foo and bar. (if the foo parameter equals to the foo string) #} +### `replace` (new in Twig 0.9.9) + +The `format` filter formats a given string by replacing the placeholders +(placeholders are free-form): + + [twig] + {# string is a format string like: I like %this% and %that%. #} + {{ string|format(['%this%': foo, '%that%': "bar"]) }} + {# returns I like foo and bar. (if the foo parameter equals to the foo string) #} + ### `cycle` The `cycle` filter can be used to cycle between an array of values: diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 6080e97..33f1d09 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -41,8 +41,9 @@ class Twig_Extension_Core extends Twig_Extension { $filters = array( // formatting filters - 'date' => new Twig_Filter_Function('twig_date_format_filter'), - 'format' => new Twig_Filter_Function('sprintf'), + 'date' => new Twig_Filter_Function('twig_date_format_filter'), + 'format' => new Twig_Filter_Function('sprintf'), + 'replace' => new Twig_Filter_Function('twig_strtr'), // encoding 'urlencode' => new Twig_Filter_Function('twig_urlencode_filter', array('is_escaper' => true)), @@ -204,6 +205,11 @@ function twig_constant_filter($constant) return constant($constant); } +function twig_strtr($pattern, $replacements) +{ + return str_replace(array_keys($replacements), array_values($replacements), $pattern); +} + /* * 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