[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
Provenance
- Commit
649c7b9- Tree
77e30ee- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
720e55b
4 files changed+96 added−5 removed
modified:docs/src/devintro.txt+29 added
88 unmodified lines89 (both located in `jinja.datastructure`) but those two types should cover90 the basic use cases. The `Undefined` object in that module exists for91 backwards compatibility and is an alias for `SilentUndefined`.92 93+To create your own undefined singleton do something like this:94+95+.. sourcecode:: jinja96+97+ from jinja.datastructure import AbstractUndefinedType, make_undefined98+99+ class MyUndefinedType(AbstractUndefindedType):100+ __slots__ = ()101+102+ def __iter__(self):103+ return iter(int, 0)104+105+ def __reduce__(self):106+ return 'MyUndefined'107+108+ MyUndefined = make_undefined(MyUndefinedType)109+110+The only thing you have to do is to override `__reduce__` so that it returns111+the name of the singleton instance and create the instance using112+`make_undefined`. Everything else is up to you. Note that currently attributes113+on undefined objects are available in the Jinja layer too which however114+will change in one of the next Jinja versions. So if you put a `foo` attribute115+on your undefined singleton you will be able to do ``{{ undefined.foo }}``116+by now but certainly not in the future.117+118+This limitation currently exists because undefined is treated as normal object119+and thus affected by normal getattr calls.120+121+122 Automatic Escaping123 ==================124 125 Jinja provides a way for automatic escaping, but we do not recommend using it.65 unmodified linesmodified:jinja/environment.py+1 added-3 removed
6 unmodified lines7 8 :copyright: 2007 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11-import re11 from jinja.lexer import Lexer12 from jinja.parser import Parser13 from jinja.loaders import LoaderWrapper14 from jinja.datastructure import SilentUndefined, Markup, Context, FakeTranslator15 from jinja.utils import collect_translations, get_attribute16 from jinja.exceptions import FilterNotFound, TestNotFound, \18- SecurityException, TemplateSyntaxError, TemplateRuntimeError17+ SecurityException, TemplateSyntaxError18 from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE19 20 21 __all__ = ['Environment']162 unmodified lines184 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 linesmodified:jinja/loaders.py+9 added-2 removed
40 unmodified lines41 sha.new('jinja(%s|%s)tmpl' %42 (name, salt or '')).hexdigest())43 44 45+46+def _loader_missing(*args, **kwargs):47+ """Helper function for `LoaderWrapper`."""48+ raise RuntimeError('no loader defined')49+50+51+52 class LoaderWrapper(object):53 """54 Wraps a loader so that it's bound to an environment.55 Also handles template syntax errors.2 unmodified lines58 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 lines103 loader is defined."""104 raise RuntimeError('no loader defined')105 106 def __nonzero__(self):100- return self.loader is not None107+ return self.available108 109 110 class BaseLoader(object):111 """425 unmodified linesadded:tests/runtime/strange.py+57 added
1+import jdebug2+from jinja import Environment, DictLoader3+4+base_tmpl = """5+{% block content %}Default{% endblock %}6+"""7+8+### condition is inside of block9+10+test1 = """11+{% extends 'base' %}12+13+{% block content %}14+ {% if False %}15+ {{ throw_exception() }}16+ {% endif %}17+{% endblock %}18+"""19+20+### block is inside of condition21+22+test2 = """23+{% extends 'base' %}24+25+{% if False %}26+ {% block content %}27+ {{ throw_exception() }}28+ {% endblock %}29+{% endif %}30+"""31+32+class TestException(Exception):33+ pass34+35+def throw_exception():36+ raise TestException()37+38+env = Environment(39+ loader=DictLoader(dict(base=base_tmpl))40+)41+42+if __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+ print54+55+ 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?'