Commits

Mike Bayer  committed 4b408e5

- The <%namespace> tag allows expressions
for the `file` argument, i.e. with ${}.
The `context` variable, if needed,
must be referenced explicitly.
[ticket:141]

- Fixed previously non-covered regular
expression, such that using a ${} expression
inside of a tag element that doesn't allow
them raises a CompileException instead of
silently failing.

  • Participants
  • Parent commits 32e5f73

Comments (0)

Files changed (5)

+0.3.5
+- The <%namespace> tag allows expressions
+  for the `file` argument, i.e. with ${}.
+  The `context` variable, if needed,
+  must be referenced explicitly.
+  [ticket:141]
+
+- Fixed previously non-covered regular 
+  expression, such that using a ${} expression 
+  inside of a tag element that doesn't allow 
+  them raises a CompileException instead of
+  silently failing.
+  
 0.3.4
 - Now using MarkupSafe for HTML escaping,
   i.e. in place of cgi.escape().  Faster

File doc/build/content/namespaces.txt

 
 **Note** - in current versions of Mako, usage of "import='*'" is known to decrease performance of the template.  This will be fixed in a future release.
 
+The `file` argument allows expressions - if looking for context variables, the `context` must be named explicitly:
+
+    <%namespace name="dyn" file="${context['namespace_name']}"/>
+    
 ### Ways to Call Namespaces {@name=howtocall}
 
 There are essentially four ways to call a function from a namespace.

File mako/parsetree.py

                             expr.append(repr(x))
                 self.parsed_attributes[key] = " + ".join(expr) or repr('')
             elif key in nonexpressions:
-                if re.search(r'${.+?}', self.attributes[key]):
+                if re.search(r'\${.+?}', self.attributes[key]):
                     raise exceptions.CompileException(
                             "Attibute '%s' in tag '%s' does not allow embedded "
                             "expressions"  % (key, self.keyword), 
     def __init__(self, keyword, attributes, **kwargs):
         super(NamespaceTag, self).__init__(
                                         keyword, attributes, 
-                                        (), 
+                                        ('file',), 
                                         ('name','inheritable',
-                                        'file','import','module'), 
+                                        'import','module'), 
                                         (), **kwargs)
                                         
         self.name = attributes.get('name', '__anon_%s' % hex(abs(id(self))))

File test/test_lexer.py

             hi.
         """
         self.assertRaises(exceptions.SyntaxException, Lexer(template).parse)
-
+    
+    def test_noexpr_allowed(self):
+        template = """
+            <%namespace name="${foo}"/>
+        """
+        self.assertRaises(exceptions.CompileException, Lexer(template).parse)
+        
     def test_unmatched_tag(self):
         template = """
         <%namespace name="bar">

File test/test_namespace.py

 from mako.template import Template
 from mako import lookup
 from util import flatten_result, result_lines
-from test import TemplateTest
+from test import TemplateTest, eq_
 
 class NamespaceTest(TemplateTest):
     def test_inline_crossreference(self):
             filters=flatten_result
         )
 
+    def test_dynamic(self):
+        collection = lookup.TemplateLookup()
+
+        collection.put_string('a', """
+        <%namespace name="b" file="${context['b_def']}"/>
+
+        a.  b: ${b.body()}
+""")
+
+        collection.put_string('b', """
+        b.
+""")
+
+        eq_(
+            flatten_result(collection.get_template('a').render(b_def='b')),
+            "a. b: b."
+        )
+        
     def test_template(self):
         collection = lookup.TemplateLookup()