optimizer can optimize filtered for loops now
--HG--
branch : trunk
diff --git a/jinja2/nodes.py b/jinja2/nodes.py
index 89c6fa2..ecc3f1e 100644
--- a/jinja2/nodes.py
+++ b/jinja2/nodes.py
@@ -35,6 +35,17 @@
'-': operator.neg
}
+_cmpop_to_func = {
+ 'eq': operator.eq,
+ 'ne': operator.ne,
+ 'gt': operator.gt,
+ 'gteq': operator.ge,
+ 'lt': operator.lt,
+ 'lteq': operator.le,
+ 'in': operator.contains,
+ 'notin': lambda a, b: not operator.contains(a, b)
+}
+
class Impossible(Exception):
"""Raised if the node could not perform a requested action."""
@@ -484,6 +495,14 @@
"""{{ foo == bar }}, {{ foo >= bar }} etc."""
fields = ('expr', 'ops')
+ def as_const(self):
+ result = value = self.expr.as_const()
+ for op in self.ops:
+ new_value = op.expr.as_const()
+ result = _cmpop_to_func[op.op](value, new_value)
+ value = new_value
+ return result
+
class Operand(Helper):
"""Operator + expression."""