added a replace filter
authorFabien Potencier <fabien.potencier@gmail.com>
Fri, 1 Oct 2010 10:08:46 +0000 (12:08 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Fri, 1 Oct 2010 12:16:02 +0000 (14:16 +0200)
CHANGELOG
doc/02-Twig-for-Template-Designers.markdown
lib/Twig/Extension/Core.php

index d045514..c647692 100644 (file)
--- 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
index c0dace8..d46c1d6 100644 (file)
@@ -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:
index 6080e97..33f1d09 100644 (file)
@@ -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