return result
def __getitem__(self, name):
- # don't give access to jinja internal variables
if name.startswith('::'):
return Undefined
# because the stack is usually quite small we better use [::-1]
for d in self._stack[::-1]:
if name in d:
rv = d[name]
- if isinstance(rv, Deferred):
+ if rv.__class__ is Deferred:
rv = rv(self, name)
# never touch the globals!
if d is self.globals:
if value is Undefined or value is None:
return u''
val = self.to_unicode(value)
- # apply default filters
if self.default_filters:
val = self.apply_filters(val, ctx, self.default_filters)
return val
:license: BSD, see LICENSE for more details.
"""
from compiler import ast
+from copy import copy
def inc_lineno(offset, tree):
"""
Create an independent clone of this node.
"""
- rv = Block(None, None, None)
- rv.__dict__.update(self.__dict__)
- return rv
+ return copy(self)
def get_items(self):
return [self.name, self.body]
# python2.4 and lower has a bug regarding joining of broken generators
-if sys.hexversion < (2, 5):
- def capture_generator(gen):
- """
- Concatenate the generator output.
- """
- return u''.join(tuple(gen))
+if sys.version_info < (2, 5):
+ capture_generator = lambda gen: u''.join(tuple(gen))
# this should be faster and used in python2.5 and higher
else:
- def capture_generator(gen):
- """
- Concatenate the generator output
- """
- return u''.join(gen)
+ capture_generator = u''.join
def buffereater(f):
import cgi
import sys
import timeit
+import jdebug
from StringIO import StringIO
from genshi.builder import tag
django_tmpl = DjangoTemplate("""
<table>
{% for row in table %}
-<tr>{% for col in row.values %}{{ col|escape }}{% endfor %}</tr>
+<tr>{% for col in row.values %}{{ col }}{% endfor %}</tr>
{% endfor %}
</table>
""")
jinja_tmpl = Environment().from_string('''
<table>
-{% for row in table %}
-<tr>{% for col in row.values() %}{{ col|escape }}{% endfor %}</tr>
+{% for row in table -%}
+<tr>{% for col in row.values() %}{{ col }}{% endfor %}</tr>
{% endfor %}
</table>
''')
cheetah_tmpl = CheetahTemplate('''
-# filter escape
<table>
#for $row in $table
<tr>
% for row in table:
<tr>
% for col in row.values():
- ${col|h}
+ ${col}
% endfor
</tr>
% endfor
which = [arg for arg in sys.argv[1:] if arg[0] != '-']
if '-p' in sys.argv:
- import hotshot, hotshot.stats
- prof = hotshot.Profile("template.prof")
- benchtime = prof.runcall(run, which, number=1)
- stats = hotshot.stats.load("template.prof")
+ from cProfile import Profile
+ from pstats import Stats
+ p = Profile()
+ p.runcall(test_jinja)
+ stats = Stats(p)
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats()