Commits

Andriy Kornatskyy committed ebdf057

Added support for import and from keywords.

Comments (0)

Files changed (2)

src/wheezy/template/ext/core.py

 end_tokens = ['end']
 continue_tokens = ['else:', 'elif ']
 compound_tokens = ['for ', 'if ', 'def ', 'extends'] + continue_tokens
-reserved_tokens = ['require', '#', 'include']
+reserved_tokens = ['require', '#', 'include', 'import ', 'from ']
 all_tokens = end_tokens + compound_tokens + reserved_tokens
 out_tokens = ['markup', 'var', 'include']
 known_var_filters = {
     return value.rstrip()[8:-1]
 
 
+def parse_import(value):
+    name, var = value[7:].rsplit(' as ', 1)
+    return name, var
+
+
+def parse_from(value):
+    name, var = value[5:].rsplit(' import ', 1)
+    s = var.rsplit(' as ', 1)
+    if len(s) == 2:
+        var, alias = s
+    else:
+        alias = var
+    return name, var, alias
+
+
 def parse_var(value):
     if '!!' not in value:
         return value, None
     return True
 
 
+def build_import(builder, lineno, token, value):
+    assert token == 'import '
+    name, var = value
+    builder.add(lineno, var + ' = ' + '_i(' + name + ')')
+    return True
+
+
+def build_from(builder, lineno, token, value):
+    assert token == 'from '
+    name, var, alias = value
+    builder.add(lineno, alias + ' = ' + '_i(' + name
+            + ').local_defs[\'' + var + '\']')
+    return True
+
+
 def build_render(builder, lineno, token, nodes):
     assert lineno <= 0
     assert token == 'render'
             'require': parse_require,
             'extends': parse_extends,
             'include': parse_include,
+            'import ': parse_import,
+            'from ': parse_from,
             'var': parse_var,
             'markup': parse_markup,
     }
     builder_rules = [
             ('render', build_extends),
             ('render', build_render),
+            ('import ', build_import),
+            ('from ', build_from),
             ('require', build_require),
             ('out', build_out),
             ('def ', build_def_empty),

src/wheezy/template/ext/tests/test_core.py

 Welcome to my site.
 Thanks, John""" == self.render('tmpl.html', ctx)
         assert 'Thanks, John' == self.render('footer.html', ctx)
+
+    def test_import(self):
+        self.templates.update({
+                'helpers.html': """\
+@def say_hi(name):
+Hi, @name\
+@end""",
+                'tmpl.html': """\
+@import 'helpers.html' as helpers
+@helpers.say_hi('John')"""
+        })
+        assert """\
+Hi, John""" == self.render('tmpl.html', {})
+
+    def test_import_dynamic(self):
+        self.templates.update({
+                'helpers.html': """\
+@def say_hi(name):
+Hi, @name\
+@end""",
+                'tmpl.html': """\
+@require(helpers_impl)
+@import helpers_impl as helpers
+@helpers.say_hi('John')"""
+        })
+        assert """\
+Hi, John""" == self.render('tmpl.html', {'helpers_impl': 'helpers.html'})
+
+    def test_from_import(self):
+        self.templates.update({
+                'helpers.html': """\
+@def say_hi(name):
+Hi, @name\
+@end""",
+                'tmpl.html': """\
+@from 'helpers.html' import say_hi
+@say_hi('John')"""
+        })
+        assert """\
+Hi, John""" == self.render('tmpl.html', {})
+
+    def test_from_import_dynamic(self):
+        self.templates.update({
+                'helpers.html': """\
+@def say_hi(name):
+Hi, @name\
+@end""",
+                'tmpl.html': """\
+@require(helpers_impl)
+@from helpers_impl import say_hi
+@say_hi('John')"""
+        })
+        assert """\
+Hi, John""" == self.render('tmpl.html', {'helpers_impl': 'helpers.html'})
+
+    def test_from_import_as(self):
+        self.templates.update({
+                'share/helpers.html': """\
+@def say_hi(name):
+Hi, @name\
+@end""",
+                'tmpl.html': """\
+@from 'share/helpers.html' import say_hi as hi
+@hi('John')"""
+        })
+        assert """\
+Hi, John""" == self.render('tmpl.html', {})