* 0.9.5-DEV
+ * added the following filters in the Core extension: in
* added support for arrays (same behavior as in PHP, a mix between lists and dictionaries, arrays and hashes)
* enhanced some error messages to provide better feedback in case of parsing errors
The `sort` filter sorts an array.
+### `in`
+
+Returns true if the value is contained within another one.
+
+ [twig]
+ {# returns true #}
+
+ {{ 1|in([1, 2, 3]) }}
+
+ {{ 'cd'|in('abcde') }}
+
+You can use this filter to perform a containment test on strings, arrays, or
+objects implementing the `ArrayAccess` interface.
+
### `default`
The `default` filter returns the passed default value if the value is
'reverse' => array('twig_reverse_filter', false),
'length' => array('twig_length_filter', false),
'sort' => array('twig_sort_filter', false),
+ 'in' => array('twig_in_filter', false),
// iteration and runtime
'default' => array('twig_default_filter', false),
return $array;
}
+function twig_in_filter($value, $compare)
+{
+ if (is_array($compare))
+ {
+ return in_array($value, $compare);
+ }
+ elseif (is_string($compare))
+ {
+ return false !== strpos($compare, (string) $value);
+ }
+ elseif (is_object($compare) && $compare instanceof ArrayAccess)
+ {
+ return in_array($value, iterator_to_array($compare));
+ }
+
+ return false;
+}
+
/*
* 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