"""
def decorator(*args):
def wrapped(env, context, value):
- args = list(args)
- for idx, var in enumerate(args):
+ nargs = list(args)
+ for idx, var in enumerate(nargs):
if isinstance(var, str):
- args[idx] = env.to_unicode(var)
- return f(env.to_unicode(value), *args)
+ nargs[idx] = env.to_unicode(var)
+ return f(env.to_unicode(value), *nargs)
return wrapped
return decorator
if count is None:
return s.replace(old, new)
return s.replace(old, new, count)
+do_replace = stringfilter(do_replace)
def do_upper(s):
Return a copy of s converted to uppercase.
"""
return s.upper()
+do_upper = stringfilter(do_upper)
def do_lower(s):
Return a copy of s converted to lowercase.
"""
return s.lower()
+do_lower = stringfilter(do_lower)
def do_escape(s, attribute=False):
if attribute:
s = s.replace('"', """)
return s
+escape = stringfilter(do_escape)
def do_addslashes(s):
Adds slashes to s.
"""
return s.encode('utf-8').encode('string-escape').decode('utf-8')
+do_addslashes = stringfilter(do_addslashes)
def do_capitalize(s):
capitalized.
"""
return s.capitalize()
+do_capitalize = stringfilter(do_capitalize)
def do_title(s):
characters, all remaining cased characters have lowercase.
"""
return s.title()
+do_title = stringfilter(do_title)
def do_default(default_value=u''):
which is '' per default.
"""
return lambda e, c, v: v or default_value
+do_default = stringfilter(do_default)
def do_join(d=u''):
Return the length of var. In case if getting an integer or float
it will convert it into a string an return the length of the new
string.
- If the object doesn't provide a __len__ function it will return
- zero.st(value)
- l.reverse()
- return
+ If the object doesn't provide a __len__ function it will return zero
"""
def wrapped(env, context, value):
try:
"""
jinja.lexer
~~~~~~~~~~~
+
+ :copyright: 2006 by Armin Ronacher.
+ :license: BSD, see LICENSE for more details.
"""
import re
from jinja.datastructure import TokenStream
todo = [tree]
while todo:
node = todo.pop()
- node.lineno = (node.lineno or 0) + offset
+ if node.lineno:
+ node.lineno += offset - 1
+ else:
+ node.lineno = offset
todo.extend(node.getChildNodes())
--- /dev/null
+# -*- coding: utf-8 -*-
+"""
+ jinja.template
+ ~~~~~~~~~~~~~~
+
+ Template class.
+
+ :copyright: 2006 by Armin Ronacher.
+ :license: BSD, see LICENSE for more details.
+
+"""
+from jinja.datastructure import Context
+
+
+class Template(object):
+ """
+ Represents a finished template.
+ """
+
+ def __init__(self, environment, generate_func):
+ self.environment = environment
+ self.generate_func = generate_func
+
+ def render(self, *args, **kwargs):
+ result = []
+ ctx = Context(self.environment, *args, **kwargs)
+ self.generate_func(ctx, result.append)
+ return u''.join(result)
args or ''
))
elif n.__class__ is ast.Name:
- filters.append('environment.prepare_filter(%s)' %
- self.handle_node(n))
+ filters.append('environment.prepare_filter(%r)' %
+ n.name)
else:
raise TemplateSyntaxError('invalid filter. filter must be a '
'hardcoded function name from the '
def translate(self):
return (
- 'from jinja.datastructures import Undefined, LoopContext, CycleContext\n'
+ 'from jinja.datastructure import Undefined, LoopContext, CycleContext\n'
'def generate(context, write, write_var=None):\n'
' environment = context.environment\n'
' if write_var is None:\n'
from jinja.parser import Parser
from jinja.translators.python import translate
print translate(e, Parser(e, x).parse())
+
+
+def load_template(x):
+ from jinja.template import Template
+ from jinja.parser import Parser
+ from jinja.translators.python import translate
+ code = translate(e, Parser(e, x).parse())
+ ns = {}
+ exec code in ns
+ return Template(e, ns['generate'])