changeset 120:c9f0a26e28a2 trunk

* Allow `py:with` directives to define `lambda`s * Fix directive order so that a `py:for` loop can be used inside a `py:when` or `py:otherwise` branch.
author cmlenz
date Wed, 02 Aug 2006 11:56:31 +0000
parents cc2aee07f53b
children 062e51ad7b19
files markup/eval.py markup/template.py
diffstat 2 files changed, 7 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/markup/eval.py
+++ b/markup/eval.py
@@ -76,17 +76,19 @@
     def __repr__(self):
         return '<Expression "%s">' % self.source
 
-    def evaluate(self, data):
+    def evaluate(self, data, nocall=False):
         """Evaluate the expression against the given data dictionary.
         
         @param data: a mapping containing the data to evaluate against
+        @param nocall: if true, the result of the evaluation is not called if
+            if it is a callable
         @return: the result of the evaluation
         """
         retval = eval(self.code, {'data': data,
                                   '_lookup_name': _lookup_name,
                                   '_lookup_attr': _lookup_attr,
                                   '_lookup_item': _lookup_item})
-        if callable(retval):
+        if not nocall and callable(retval):
             retval = retval()
         return retval
 
--- a/markup/template.py
+++ b/markup/template.py
@@ -648,7 +648,7 @@
                                       offset + (err.offset or 0))
 
     def __call__(self, stream, ctxt, directives):
-        ctxt.push(dict([(name, expr.evaluate(ctxt))
+        ctxt.push(dict([(name, expr.evaluate(ctxt, nocall=True))
                         for name, expr in self.vars]))
         for event in _apply_directives(stream, ctxt, directives):
             yield event
@@ -671,10 +671,10 @@
 
     directives = [('def', DefDirective),
                   ('match', MatchDirective),
+                  ('when', WhenDirective),
+                  ('otherwise', OtherwiseDirective),
                   ('for', ForDirective),
                   ('if', IfDirective),
-                  ('when', WhenDirective),
-                  ('otherwise', OtherwiseDirective),
                   ('choose', ChooseDirective),
                   ('with', WithDirective),
                   ('replace', ReplaceDirective),
Copyright (C) 2012-2017 Edgewall Software