"""
from jinja2.filters import FILTERS as DEFAULT_FILTERS
from jinja.tests import TESTS as DEFAULT_TESTS
-DEFAULT_NAMESPACE = {}
+DEFAULT_NAMESPACE = {
+ 'range': xrange
+}
__all__ = ['DEFAULT_FILTERS', 'DEFAULT_TESTS', 'DEFAULT_NAMESPACE']
"""Represents a template."""
def __init__(self, environment, code):
- namespace = {'environment': environment}
+ namespace = {
+ 'environment': environment
+ }
exec code in namespace
self.environment = environment
self.name = namespace['filename']
except ImportError:
itemgetter = lambda a: lambda b: b[a]
from urllib import urlencode, quote
-from jinja2.utils import escape
+from jinja2.utils import escape, pformat
+from jinja2.nodes import Undefined
+
_striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)')
return unicode(s).title()
-def do_dictsort(case_sensitive=False, by='key'):
+def do_dictsort(value, case_sensitive=False, by='key'):
"""
Sort a dict and yield (key, value) pairs. Because python dicts are
unsorted you may want to use this function to order them by either
else:
raise FilterArgumentError('You can only sort by either '
'"key" or "value"')
- def sort_func(value, env):
+ def sort_func(value):
if isinstance(value, basestring):
- value = env.to_unicode(value)
+ value = unicode(value)
if not case_sensitive:
value = value.lower()
return value
- def wrapped(env, context, value):
- items = value.items()
- items.sort(lambda a, b: cmp(sort_func(a[pos], env),
- sort_func(b[pos], env)))
- return items
- return wrapped
+ items = value.items()
+ items.sort(lambda a, b: cmp(sort_func(a[pos]), sort_func(b[pos])))
+ return items
def do_default(value, default_value=u'', boolean=False):
{{ ''|default('the string was empty', true) }}
"""
- # XXX: undefined_sigleton
- if (boolean and not value) or value in (env.undefined_singleton, None):
+ if (boolean and not value) or isinstance(value, Undefined):
return default_value
return value
return unicode(d).join([unicode(x) for x in value])
-def do_count():
+def do_count(value):
"""
Return the length of the value. In case if getting an integer or float
it will convert it into a string an return the length of the new
return 0
-def do_reverse(l):
+def do_reverse(value):
"""
Return a reversed list of the sequence filtered. You can use this
for example for reverse iteration:
return env.undefined_singleton
-def do_random():
+def do_random(seq):
"""
Return a random item from the sequence.
"""
return simplejson.dumps(value)
-def do_filesizeformat():
+def do_filesizeformat(value):
"""
Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102
bytes, etc).
"""
- def wrapped(env, context, value):
- # fail silently
- try:
- bytes = float(value)
- except TypeError:
- bytes = 0
-
- if bytes < 1024:
- return "%d Byte%s" % (bytes, bytes != 1 and 's' or '')
- elif bytes < 1024 * 1024:
- return "%.1f KB" % (bytes / 1024)
- elif bytes < 1024 * 1024 * 1024:
- return "%.1f MB" % (bytes / (1024 * 1024))
- return "%.1f GB" % (bytes / (1024 * 1024 * 1024))
- return wrapped
+ # fail silently
+ try:
+ bytes = float(value)
+ except TypeError:
+ bytes = 0
+
+ if bytes < 1024:
+ return "%d Byte%s" % (bytes, bytes != 1 and 's' or '')
+ elif bytes < 1024 * 1024:
+ return "%.1f KB" % (bytes / 1024)
+ elif bytes < 1024 * 1024 * 1024:
+ return "%.1f MB" % (bytes / (1024 * 1024))
+ return "%.1f GB" % (bytes / (1024 * 1024 * 1024))
def do_pprint(value, verbose=False):
parts = publish_parts(source=s, writer_name='html4css1')
return parts['fragment']
-def do_int(default=0):
+
+def do_int(value, default=0):
"""
Convert the value into an integer. If the
conversion doesn't work it will return ``0``. You can
override this default using the first parameter.
"""
- def wrapped(env, context, value):
+ try:
+ return int(value)
+ except (TypeError, ValueError):
try:
- return int(value)
+ return int(float(value))
except (TypeError, ValueError):
- try:
- return int(float(value))
- except (TypeError, ValueError):
- return default
- return wrapped
+ return default
-def do_float(default=0.0):
+def do_float(value, default=0.0):
"""
Convert the value into a floating point number. If the
conversion doesn't work it will return ``0.0``. You can
override this default using the first parameter.
"""
- def wrapped(env, context, value):
- try:
- return float(value)
- except (TypeError, ValueError):
- return default
- return wrapped
+ try:
+ return float(value)
+ except (TypeError, ValueError):
+ return default
-def do_string():
+def do_string(value):
"""
Convert the value into an string.
"""
- return lambda e, c, v: e.to_unicode(v)
+ return unicode(value)
-def do_format(*args):
+def do_format(value, *args):
"""
Apply python string formatting on an object:
Note that you cannot use the mapping syntax (``%(name)s``)
like in python. Use `|dformat` for that.
"""
- def wrapped(env, context, value):
- return env.to_unicode(value) % args
- return wrapped
+ return unicode(value) % args
def do_dformat(d):
return value.strip()
-def do_capture(name='captured', clean=False):
- """
- Store the value in a variable called ``captured`` or a variable
- with the name provided. Useful for filter blocks:
-
- .. sourcecode:: jinja
-
- {% filter capture('foo') %}
- ...
- {% endfilter %}
- {{ foo }}
-
- This will output "..." two times. One time from the filter block
- and one time from the variable. If you don't want the filter to
- output something you can use it in `clean` mode:
-
- .. sourcecode:: jinja
-
- {% filter capture('foo', True) %}
- ...
- {% endfilter %}
- {{ foo }}
- """
- if not isinstance(name, basestring):
- raise FilterArgumentError('You can only capture into variables')
- def wrapped(env, context, value):
- context[name] = value
- if clean:
- return TemplateData()
- return value
- return wrapped
-
-
def do_striptags(value):
"""
Strip SGML/XML tags and replace adjacent whitespace by one space.
return ' '.join(_striptags_re.sub('', value).split())
-def do_slice(slices, fill_with=None):
+def do_slice(value, slices, fill_with=None):
"""
Slice an iterator and return a list of lists containing
those items. Useful if you want to create a div containing
*new in Jinja 1.1*
"""
- def wrapped(env, context, value):
- result = []
- seq = list(value)
- length = len(seq)
- items_per_slice = length // slices
- slices_with_extra = length % slices
- offset = 0
- for slice_number in xrange(slices):
- start = offset + slice_number * items_per_slice
- if slice_number < slices_with_extra:
- offset += 1
- end = offset + (slice_number + 1) * items_per_slice
- tmp = seq[start:end]
- if fill_with is not None and slice_number >= slices_with_extra:
- tmp.append(fill_with)
- result.append(tmp)
- return result
- return wrapped
-
-
-def do_batch(linecount, fill_with=None):
+ result = []
+ seq = list(value)
+ length = len(seq)
+ items_per_slice = length // slices
+ slices_with_extra = length % slices
+ offset = 0
+ for slice_number in xrange(slices):
+ start = offset + slice_number * items_per_slice
+ if slice_number < slices_with_extra:
+ offset += 1
+ end = offset + (slice_number + 1) * items_per_slice
+ tmp = seq[start:end]
+ if fill_with is not None and slice_number >= slices_with_extra:
+ tmp.append(fill_with)
+ result.append(tmp)
+ return result
+
+
+def do_batch(value, linecount, fill_with=None):
"""
A filter that batches items. It works pretty much like `slice`
just the other way round. It returns a list of lists with the
*new in Jinja 1.1*
"""
- def wrapped(env, context, value):
- result = []
- tmp = []
- for item in value:
- if len(tmp) == linecount:
- result.append(tmp)
- tmp = []
- tmp.append(item)
- if tmp:
- if fill_with is not None and len(tmp) < linecount:
- tmp += [fill_with] * (linecount - len(tmp))
+ result = []
+ tmp = []
+ for item in value:
+ if len(tmp) == linecount:
result.append(tmp)
- return result
- return wrapped
+ tmp = []
+ tmp.append(item)
+ if tmp:
+ if fill_with is not None and len(tmp) < linecount:
+ tmp += [fill_with] * (linecount - len(tmp))
+ result.append(tmp)
+ return result
def do_sum():
'urlize': do_urlize,
'format': do_format,
'dformat': do_dformat,
- 'capture': do_capture,
'trim': do_trim,
'striptags': do_striptags,
'slice': do_slice,
raise Impossible()
filter = self.environment.filters.get(self.name)
if filter is None or getattr(filter, 'contextfilter', False):
- raise nodes.Impossible()
+ raise Impossible()
if obj is None:
obj = self.node.as_const()
args = [x.as_const() for x in self.args]
try:
return filter(obj, *args, **kwargs)
except:
- raise nodes.Impossible()
+ raise Impossible()
class Test(Expr):
try:
return obj(*args, **kwargs)
except:
- raise nodes.Impossible()
+ raise Impossible()
class Subscript(Expr):
.replace('>', '>') \
.replace('<', '<') \
.replace('"', '"')
+
+
+def pformat(obj, verbose=False):
+ """
+ Prettyprint an object. Either use the `pretty` library or the
+ builtin `pprint`.
+ """
+ try:
+ from pretty import pretty
+ return pretty(obj, verbose=verbose)
+ except ImportError:
+ from pprint import pformat
+ return pformat(obj)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import py
-from jinja import Environment
-from jinja.parser import Parser
+from jinja2 import Environment
+from jinja2.parser import Parser
try:
# This code adds support for coverage.py (see
import coverage, atexit
- IGNORED_MODULES = ['jinja._speedups', 'jinja.defaults',
- 'jinja.translators']
+ IGNORED_MODULES = ['jinja2._speedups', 'jinja2.defaults',
+ 'jinja2.translators']
def report_coverage():
coverage.stop()
module_list = [
mod for name, mod in sys.modules.copy().iteritems() if
getattr(mod, '__file__', None) and
- name.startswith('jinja.') and
+ name.startswith('jinja2.') and
name not in IGNORED_MODULES
]
module_list.sort()
loader = GlobalLoader(globals())
-simple_env = Environment(trim_blocks=True, friendly_traceback=False, loader=loader)
+simple_env = Environment(trim_blocks=True, loader=loader)
class MemcacheClient(object):
import jdebug
from time import time
-from jinja import Environment
+from jinja2 import Environment
tmpl = Environment().from_string('''
<h1>Bigtable</h1>
<table>
except ImportError:
have_genshi = False
-from jinja import Environment
+from jinja2 import Environment
try:
from django.conf import settings
import jdebug
-from jinja import from_string
+from jinja2 import from_string
template = from_string(u'''\
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
import jdebug
-from jinja import Environment, DictLoader
-from jinja.exceptions import TemplateNotFound
+from jinja2 import Environment, DictLoader
+from jinja2.exceptions import TemplateNotFound
from wsgiref.simple_server import make_server
e = Environment(loader=DictLoader({
-from jinja import Environment, FileSystemLoader
+from jinja2 import Environment, FileSystemLoader
e = Environment(loader=FileSystemLoader('templates'))
-from jinja.parser import Parser
-from jinja.translators.python import PythonTranslator
+from jinja2.parser import Parser
+from jinja2.translators.python import PythonTranslator
tmpl = e.loader.load('c.html')
print tmpl.render()
-from jinja import Environment, FileSystemLoader
+from jinja2 import Environment, FileSystemLoader
e = Environment(loader=FileSystemLoader('templates'))
-from jinja.parser import Parser
-from jinja.translators.python import PythonTranslator
+from jinja2.parser import Parser
+from jinja2.translators.python import PythonTranslator
print PythonTranslator(e, e.loader.parse('index.html')).translate()
# test file for block super support
import jdebug
-from jinja import Environment, DictLoader
+from jinja2 import Environment, DictLoader
env = Environment(loader=DictLoader({
'a': '''\
import jdebug
-from jinja import Environment, DictLoader
+from jinja2 import Environment, DictLoader
base_tmpl = """
{% block content %}Default{% endblock %}
# test file for block super support
import jdebug
-from jinja import Environment, DictLoader
+from jinja2 import Environment, DictLoader
env = Environment(loader=DictLoader({
'a': '{% block intro %}INTRO{% endblock %}|BEFORE|{% block data %}INNER{% endblock %}|AFTER',
"""
CAPITALIZE = '''{{ "foo bar"|capitalize }}'''
-CAPTURE = '''{{ "foo"|capture('bar') }}|{{ bar }}'''
CENTER = '''{{ "foo"|center(9) }}'''
DEFAULT = '''{{ missing|default("no") }}|{{ false|default('no') }}|\
{{ false|default('no', true) }}|{{ given|default("no") }}'''
assert tmpl.render() == 'Foo bar'
-def test_capture(env):
- tmpl = env.from_string(CAPTURE)
- assert tmpl.render() == 'foo|foo'
-
-
def test_center(env):
tmpl = env.from_string(CENTER)
assert tmpl.render() == ' foo '
def test_escape(env):
tmpl = env.from_string(ESCAPE)
out = tmpl.render()
- assert out == '<">&|<">&'
+ assert out == '<">&|<">&'
def test_striptags(env):
def test_pprint(env):
from pprint import pformat
tmpl = env.from_string(PPRINT)
- data = range(10000)
+ data = range(1000)
assert tmpl.render(data=data) == pformat(data)
:copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
-from jinja import Environment, DictLoader
+from jinja2 import Environment, DictLoader
templates = {
'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'
:copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
-from jinja import Environment, DictLoader
-from jinja.exceptions import TemplateSyntaxError
+from jinja2 import Environment, DictLoader
+from jinja2.exceptions import TemplateSyntaxError
LAYOUTTEMPLATE = '''\
|{% block block1 %}block 1 from layout{% endblock %}
def test_balancing():
- from jinja import Environment
+ from jinja2 import Environment
env = Environment('{%', '%}', '${', '}')
tmpl = env.from_string(BALANCING)
assert tmpl.render(seq=range(3)) == "{'FOO': 0}{'FOO': 1}{'FOO': 2}"
def test_comments():
- from jinja import Environment
+ from jinja2 import Environment
env = Environment('<!--', '-->', '{', '}')
tmpl = env.from_string(COMMENTS)
assert tmpl.render(seq=range(3)) == ("<ul>\n <li>0</li>\n "
def test_operators(env):
- from jinja.lexer import operators
+ from jinja2.lexer import operators
for test, expect in operators.iteritems():
if test in '([{}])':
continue
import time
import tempfile
-from jinja import Environment, loaders
-from jinja.exceptions import TemplateNotFound
+from jinja2 import Environment, loaders
+from jinja2.exceptions import TemplateNotFound
dict_loader = loaders.DictLoader({
def test_kwargs_failure(env):
- from jinja.exceptions import TemplateRuntimeError
+ from jinja2.exceptions import TemplateRuntimeError
tmpl = env.from_string(KWARGSFAILURE)
try:
tmpl.render()
:license: BSD, see LICENSE for more details.
"""
-from jinja import Environment
+from jinja2 import Environment
NO_VARIABLE_BLOCK = '''\
{# i'm a freaking comment #}\
:copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
-from jinja import Environment
+from jinja2 import Environment
NONLOCALSET = '''\
:copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
-from jinja import Environment, DictLoader
-from jinja.exceptions import TemplateSyntaxError
+from jinja2 import Environment, DictLoader
+from jinja2.exceptions import TemplateSyntaxError
CALL = '''{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}'''
def test_call():
- from jinja import Environment
+ from jinja2 import Environment
env = Environment()
env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g
tmpl = env.from_string(CALL)
:license: BSD, see LICENSE for more details.
"""
-from jinja import Environment
-from jinja.exceptions import TemplateRuntimeError
-from jinja.datastructure import SilentUndefined, ComplainingUndefined
+from jinja2 import Environment
+from jinja2.exceptions import TemplateRuntimeError
+from jinja2.datastructure import SilentUndefined, ComplainingUndefined
silent_env = Environment(undefined_singleton=SilentUndefined)
:copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
-from jinja.exceptions import TemplateSyntaxError
+from jinja2.exceptions import TemplateSyntaxError
KEYWORDS = '''\
{{ with }}
def test_crazy_raw():
- from jinja import Environment
+ from jinja2 import Environment
env = Environment('{', '}', '{', '}')
tmpl = env.from_string('{raw}{broken foo}{endraw}')
assert tmpl.render() == '{broken foo}'
def test_cache_dict():
- from jinja.utils import CacheDict
+ from jinja2.utils import CacheDict
d = CacheDict(3)
d["a"] = 1
d["b"] = 2
def test_stringfilter(env):
- from jinja.filters import stringfilter
+ from jinja2.filters import stringfilter
f = stringfilter(lambda f, x: f + x)
assert f('42')(env, None, 23) == '2342'
def test_simplefilter(env):
- from jinja.filters import simplefilter
+ from jinja2.filters import simplefilter
f = simplefilter(lambda f, x: f + x)
assert f(42)(env, None, 23) == 65