* 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
### `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:
{
$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)),
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