changeset 90:c835e81c50af trunk

When an expression evaluates to a callable, it is called implicitly.
author cmlenz
date Wed, 19 Jul 2006 17:50:17 +0000
parents 80386d62814f
children a71a58df6bf5
files markup/eval.py markup/template.py markup/tests/eval.py markup/tests/template.py
diffstat 4 files changed, 32 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/markup/eval.py
+++ b/markup/eval.py
@@ -82,7 +82,10 @@
         @param data: a mapping containing the data to evaluate against
         @return: the result of the evaluation
         """
-        return eval(self.code)
+        retval = eval(self.code)
+        if callable(retval):
+            retval = retval()
+        return retval
 
     def _compile(self, source, filename, lineno):
         tree = parse(self.source, 'eval')
--- a/markup/template.py
+++ b/markup/template.py
@@ -273,26 +273,29 @@
     ...   <p py:def="echo(greeting, name='world')" class="message">
     ...     ${greeting}, ${name}!
     ...   </p>
-    ...   ${echo('hi', name='you')}
+    ...   ${echo('Hi', name='you')}
     ... </div>''')
     >>> print tmpl.generate(ctxt)
     <div>
       <p class="message">
-        hi, you!
+        Hi, you!
       </p>
     </div>
     
+    If a function does not require parameters, the parenthesis can be omitted
+    both when defining and when calling it:
+    
     >>> ctxt = Context(bar='Bye')
     >>> tmpl = Template('''<div xmlns:py="http://markup.edgewall.org/">
-    ...   <p py:def="echo(greeting, name='world')" class="message">
-    ...     ${greeting}, ${name}!
+    ...   <p py:def="helloworld" class="message">
+    ...     Hello, world!
     ...   </p>
-    ...   <div py:replace="echo('hello')"></div>
+    ...   ${helloworld}
     ... </div>''')
     >>> print tmpl.generate(ctxt)
     <div>
       <p class="message">
-        hello, world!
+        Hello, world!
       </p>
     </div>
     """
--- a/markup/tests/eval.py
+++ b/markup/tests/eval.py
@@ -180,6 +180,12 @@
         data = {'foo': {'bar': range(42)}}
         self.assertEqual(42, Expression("len(foo.bar)").evaluate(data))
 
+    def test_call_function_without_params(self):
+        self.assertEqual(42, Expression("foo").evaluate({'foo': lambda: 42}))
+        data = {'foo': 'bar'}
+        self.assertEqual('BAR', Expression("foo.upper").evaluate(data))
+        data = {'foo': {'bar': range(42)}}
+
     def test_list_comprehension(self):
         expr = Expression("[n for n in numbers if n < 2]")
         self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))
--- a/markup/tests/template.py
+++ b/markup/tests/template.py
@@ -155,6 +155,19 @@
             <b>foo</b>
         </doc>""", str(tmpl.generate()))
 
+    def test_exec_in_replace(self):
+        tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
+          <p py:def="echo(greeting, name='world')" class="message">
+            ${greeting}, ${name}!
+          </p>
+          <div py:replace="echo('hello')"></div>
+        </div>""")
+        self.assertEqual("""<div>
+          <p class="message">
+            hello, world!
+          </p>
+        </div>""", str(tmpl.generate()))
+
     def test_as_element(self):
         """
         Verify that the directive can also be used as an element.
Copyright (C) 2012-2017 Edgewall Software