From: Armin Ronacher Date: Fri, 11 Apr 2008 14:40:09 +0000 (+0200) Subject: w000t. first template X-Git-Tag: 2.0rc1~186 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=46f5f982fcd2d9a44d9c4b98cc7e718d44cc875d;p=jinja2.git w000t. first template --HG-- branch : trunk --- diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 6175916..ba9b5ed 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -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(')') diff --git a/jinja2/environment.py b/jinja2/environment.py index a350620..977e8d1 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -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=''): + """Load a template from a string.""" + return Template(self, self.compile(source, filename)) + class Template(object): """Represents a template.""" diff --git a/jinja2/loaders.py b/jinja2/loaders.py index 796b49f..95edfbc 100644 --- a/jinja2/loaders.py +++ b/jinja2/loaders.py @@ -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 index 0000000..edacc9c --- /dev/null +++ b/test.py @@ -0,0 +1,12 @@ +from jinja2 import Environment + +env = Environment() +tmpl = env.from_string(""" + +""") + +print tmpl.render(seq=range(10))