Skip to content
Myr

[svn] some minor updates in jinja

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 720e55b · Browse files · 4 files+96 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+96 added−5 removed

  • tests:+57 added−0 removed
  • other:+39 added−5 removed

4 files+96 added−5 removed

modified:docs/src/devintro.txt+29 added
88 unmodified lines
89(both located in `jinja.datastructure`) but those two types should cover90the basic use cases. The `Undefined` object in that module exists for91backwards compatibility and is an alias for `SilentUndefined`.9293To create your own undefined singleton do something like this:9495.. sourcecode:: jinja9697 from jinja.datastructure import AbstractUndefinedType, make_undefined9899 class MyUndefinedType(AbstractUndefindedType):100 __slots__ = ()101102 def __iter__(self):103 return iter(int, 0)104105 def __reduce__(self):106 return 'MyUndefined'107108 MyUndefined = make_undefined(MyUndefinedType)109110The only thing you have to do is to override `__reduce__` so that it returns111the name of the singleton instance and create the instance using112`make_undefined`. Everything else is up to you. Note that currently attributes113on undefined objects are available in the Jinja layer too which however114will change in one of the next Jinja versions. So if you put a `foo` attribute115on your undefined singleton you will be able to do ``{{ undefined.foo }}``116by now but certainly not in the future.117118This limitation currently exists because undefined is treated as normal object119and thus affected by normal getattr calls.120121122Automatic Escaping123==================124125Jinja provides a way for automatic escaping, but we do not recommend using it.
65 unmodified lines
modified:jinja/environment.py+1 added-3 removed
6 unmodified lines
78 :copyright: 2007 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import re11from jinja.lexer import Lexer12from jinja.parser import Parser13from jinja.loaders import LoaderWrapper14from jinja.datastructure import SilentUndefined, Markup, Context, FakeTranslator15from jinja.utils import collect_translations, get_attribute16from jinja.exceptions import FilterNotFound, TestNotFound, \18 SecurityException, TemplateSyntaxError, TemplateRuntimeError17 SecurityException, TemplateSyntaxError18from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE192021__all__ = ['Environment']
162 unmodified lines
184 Load and parse a template source and translate it into eval-able185 Python code. This code is wrapped within a `Template` class that186 allows you to render it.187 """189 from jinja.parser import Parser188 from jinja.translators.python import PythonTranslator189 try:190 rv = PythonTranslator.process(self, Parser(self, source).parse())191 except TemplateSyntaxError, e:
163 unmodified lines
modified:jinja/loaders.py+9 added-2 removed
40 unmodified lines
41 sha.new('jinja(%s|%s)tmpl' %42 (name, salt or '')).hexdigest())43444546def _loader_missing(*args, **kwargs):47 """Helper function for `LoaderWrapper`."""48 raise RuntimeError('no loader defined')49505152class LoaderWrapper(object):53 """54 Wraps a loader so that it's bound to an environment.55 Also handles template syntax errors.
2 unmodified lines
58 def __init__(self, environment, loader):59 self.environment = environment60 self.loader = loader61 if self.loader is None:55 self.get_source = self.parse = self.load = self._loader_missing62 self.get_source = self.parse = self.load = _loader_missing63 self.available = False64 else:65 self.available = True66
36 unmodified lines
103 loader is defined."""104 raise RuntimeError('no loader defined')105106 def __nonzero__(self):100 return self.loader is not None107 return self.available108109110class BaseLoader(object):111 """
425 unmodified lines
added:tests/runtime/strange.py+57 added
1import jdebug2from jinja import Environment, DictLoader34base_tmpl = """5{% block content %}Default{% endblock %}6"""78### condition is inside of block910test1 = """11{% extends 'base' %}1213{% block content %}14 {% if False %}15 {{ throw_exception() }}16 {% endif %}17{% endblock %}18"""1920### block is inside of condition2122test2 = """23{% extends 'base' %}2425{% if False %}26 {% block content %}27 {{ throw_exception() }}28 {% endblock %}29{% endif %}30"""3132class TestException(Exception):33 pass3435def throw_exception():36 raise TestException()3738env = Environment(39 loader=DictLoader(dict(base=base_tmpl))40)4142if __name__ == '__main__':43 for name in 'test1', 'test2':44 template_body = globals().get(name)45 template = env.from_string(template_body)46 try:47 print 'Rendering template:\n"""%s"""' % template_body48 template.render(throw_exception=throw_exception)49 except TestException:50 print 'Result: throw_exception() was called'51 else:52 print 'Result: throw_exception() was not called'53 print5455 print 'First template illustrates that condition is working well'56 print 'The question is - why {% block %} is being evalueted '\57 'in false condition in second template?'