Skip to content
Myr

[svn] added a loader that loads templates from functions provided

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 93e14c2 · Browse files · 2 files+45 added−2 removed
This commit

State

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

Diff stats

2 files+45 added−2 removed

  • other:+45 added−2 removed

2 files+45 added−2 removed

modified:jinja/loaders.py+44 added-1 removed
23 unmodified lines
24except ImportError:25 resource_exists = resource_string = resource_filename = None262728__all__ = ['FileSystemLoader', 'PackageLoader', 'DictLoader', 'ChoiceLoader']28__all__ = ['FileSystemLoader', 'PackageLoader', 'DictLoader', 'ChoiceLoader',29 'FunctionLoader']303132def get_template_filename(searchpath, name):33 """
299 unmodified lines
333 return path.getmtime(fn)334 return 0335336337class FunctionLoader(CachedLoaderMixin, BaseLoader):338 """339 Loads templates by calling a function which has to return a string340 or `None` if an error occoured.341342 .. sourcecode:: python343344 from jinja import Environment, FunctionLoader345346 def my_load_func(template_name):347 if template_name == 'foo':348 return '...'349350 e = Environment(loader=FunctionLoader(my_load_func))351352 Because the interface is limited there is no way to cache such353 templates. Usually you should try to use a loader with a more354 solid backend.355356 You can pass the following keyword arguments to the loader on357 initialisation:358359 =================== =================================================360 ``loader_func`` Function that takes the name of the template to361 load. If it returns a string or unicode object362 it's used to load a template. If the return363 value is None it's considered missing.364 =================== =================================================365 """366367 def __init__(self, loader_func):368 self.loader_func = loader_func369370 def get_source(self, environment, name, parent):371 rv = self.loader_func(name)372 if rv is None:373 raise TemplateNotFound(name)374 if isinstance(rv, str):375 return rv.decode(environment.template_charset)376 return rv377378379class DictLoader(BaseLoader):380 """381 Load templates from a given dict:382
57 unmodified lines
modified:jinja/utils.py+1 added-1 removed
162 unmodified lines
163164def raise_template_exception(exception, filename, lineno, context):165 """166 Raise an exception "in a template". Return a traceback167 object.167 object. This is used for runtime debugging, not compile time.168 """169 # some traceback systems allow to skip frames170 __traceback_hide__ = True171
218 unmodified lines