w000t. first template
authorArmin Ronacher <armin.ronacher@active-4.com>
Fri, 11 Apr 2008 14:40:09 +0000 (16:40 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Fri, 11 Apr 2008 14:40:09 +0000 (16:40 +0200)
--HG--
branch : trunk

jinja2/compiler.py
jinja2/environment.py
jinja2/loaders.py
test.py [new file with mode: 0644]

index 61759167254a88160d91a4c11489253c6f51739d..ba9b5ed536afe6ad9d039232a2b72a2fe3e5c439 100644 (file)
@@ -279,7 +279,7 @@ class CodeGenerator(NodeVisitor):
             self.writeline('l_%s = context[%r]' % (name, name))
         for name, count in frame.identifiers.undeclared_filter.iteritems():
             if count > 1:
-                self.writeline('f_%s = context[%r]' % (name, name))
+                self.writeline('f_%s = environment.filters[%r]' % (name, name))
         if not no_indent:
             self.outdent()
 
@@ -714,13 +714,13 @@ class CodeGenerator(NodeVisitor):
         if node.name in frame.identifiers.declared_filter:
             self.write('f_%s(' % node.name)
         else:
-            self.write('context.filter[%r](' % node.name)
+            self.write('environment.filters[%r](' % node.name)
         self.visit(node.node, frame)
         self.signature(node, frame)
         self.write(')')
 
     def visit_Test(self, node, frame):
-        self.write('context.tests[%r](')
+        self.write('environment.tests[%r](')
         self.visit(node.node, frame)
         self.signature(node, frame)
         self.write(')')
index a350620535c40f88611e0460d9cef17f8d59617c..977e8d1f21a981b944ef1100e421eab14782f8de 100644 (file)
@@ -104,9 +104,10 @@ class Environment(object):
         if isinstance(source, basestring):
             source = self.parse(source, filename)
         node = optimize(source, self)
-        source = generate(node, self)
+        source = generate(node, self, filename)
         if raw:
             return source
+        print source
         if isinstance(filename, unicode):
             filename = filename.encode('utf-8')
         return compile(source, filename, 'exec')
@@ -125,6 +126,10 @@ class Environment(object):
             name = self.join_path(name, parent)
         return self.loader.load(self, name)
 
+    def from_string(self, source, filename='<string>'):
+        """Load a template from a string."""
+        return Template(self, self.compile(source, filename))
+
 
 class Template(object):
     """Represents a template."""
index 796b49f908e584925e23845494cdf9372d608e75..95edfbc1299c8ecbd4337f9e4d8c5c07547bb07f 100644 (file)
@@ -14,6 +14,7 @@ from jinja2.environment import Template
 
 
 class BaseLoader(object):
+    """Baseclass for all loaders."""
 
     def get_source(self, environment, template):
         raise TemplateNotFound()
diff --git a/test.py b/test.py
new file mode 100644 (file)
index 0000000..edacc9c
--- /dev/null
+++ b/test.py
@@ -0,0 +1,12 @@
+from jinja2 import Environment
+
+env = Environment()
+tmpl = env.from_string("""
+<ul>
+{% for item in seq %}
+    <li>{{ item|e }}</li>
+{% endfor %}
+</ul>
+""")
+
+print tmpl.render(seq=range(10))