* the items filter, which has been deprecated for quite a long time now, has been removed
* the range filter has been converted to a function: 0|range(10) -> range(0, 10)
* the constant filter has been converted to a function: {{ some_date|date('DATE_W3C'|constant) }} -> {{ some_date|date(constant('DATE_W3C')) }}
+ * the cycle filter has been converted to a function: {{ ['odd', 'even']|cycle(i) }} -> {{ cycle(['odd', 'even'], i) }}
Changes:
{# 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:
-
-.. code-block:: jinja
-
- {% for i in 0..10 %}
- {{ ['odd', 'even']|cycle(i) }}
- {% endfor %}
-
-The array can contain any number of values:
-
-.. code-block:: jinja
-
- {% set fruits = ['apple', 'orange', 'citrus'] %}
-
- {% for i in 0..10 %}
- {{ fruits|cycle(i) }}
- {% endfor %}
-
``url_encode``
~~~~~~~~~~~~~~
{{ i }},
{% endfor %}
+``cycle``
+~~~~~~~~~
+
+The ``cycle`` function can be used to cycle on an array of values:
+
+.. code-block:: jinja
+
+ {% for i in 0..10 %}
+ {{ cycle(['odd', 'even'], i) }}
+ {% endfor %}
+
+The array can contain any number of values:
+
+.. code-block:: jinja
+
+ {% set fruits = ['apple', 'orange', 'citrus'] %}
+
+ {% for i in 0..10 %}
+ {{ cycle(fruits, i) }}
+ {% endfor %}
+
``constant``
~~~~~~~~~~~~
'reverse' => new Twig_Filter_Function('twig_reverse_filter'),
'length' => new Twig_Filter_Function('twig_length_filter', array('needs_environment' => true)),
'sort' => new Twig_Filter_Function('twig_sort_filter'),
- 'cycle' => new Twig_Filter_Function('twig_cycle_filter'),
'merge' => new Twig_Filter_Function('twig_array_merge'),
// iteration and runtime
public function getGlobals()
{
return array(
- 'fn_range' => new Twig_Function($this, 'getRange'),
+ 'fn_range' => new Twig_Function($this, 'getRange'),
'fn_constant' => new Twig_Function($this, 'getConstant'),
+ 'fn_cycle' => new Twig_Function($this, 'getCycle'),
);
}
return constant($value);
}
+ public function getCycle($values, $i)
+ {
+ if (!is_array($values) && !$values instanceof ArrayAccess) {
+ return $values;
+ }
+
+ return $values[$i % count($values)];
+ }
+
/**
* Returns a list of filters to add to the existing list.
*
return false;
}
-function twig_cycle_filter($values, $i)
-{
- if (!is_array($values) && !$values instanceof ArrayAccess) {
- return $values;
- }
-
- return $values[$i % count($values)];
-}
-
function twig_strtr($pattern, $replacements)
{
return str_replace(array_keys($replacements), array_values($replacements), $pattern);
--TEST--
-"cycle" filter
+"cycle" function
--TEMPLATE--
{% for i in 0..6 %}
-{{ array1|cycle(i) }}-{{ array2|cycle(i) }}
+{{ cycle(array1, i) }}-{{ cycle(array2, i) }}
{% endfor %}
--DATA--
return array('array1' => array('odd', 'even'), 'array2' => array('apple', 'orange', 'citrus'))
--DATA--
return array('foo' => 'bar')
--EXPECT--
-fn_range,fn_constant,foo,_parent,
-fn_range,fn_constant,_parent,
-fn_range,fn_constant,foo,foo1,_parent,
-fn_range,fn_constant,foo1,_parent,
+fn_range,fn_constant,fn_cycle,foo,_parent,
+fn_range,fn_constant,fn_cycle,_parent,
+fn_range,fn_constant,fn_cycle,foo,foo1,_parent,
+fn_range,fn_constant,fn_cycle,foo1,_parent,