class PackageLoader(BaseLoader):
"""Load templates from python eggs or packages. It is constructed with
the name of the python package and the path to the templates in that
- package:
+ package::
- >>> loader = PackageLoader('mypackage', 'views')
+ loader = PackageLoader('mypackage', 'views')
If the package path is not given, ``'templates'`` is assumed.
filename, uptodatefunc)`` or `None` if the template does not exist.
>>> def load_template(name):
- ... if name == 'index.html'
+ ... if name == 'index.html':
... return '...'
...
>>> loader = FunctionLoader(load_template)
"""A loader that is passed a dict of loaders where each loader is bound
to a prefix. The prefix is delimited from the template by a slash per
default, which can be changed by setting the `delimiter` argument to
- something else.
+ something else::
- >>> loader = PrefixLoader({
- ... 'app1': PackageLoader('mypackage.app1'),
- ... 'app2': PackageLoader('mypackage.app2')
- ... })
+ loader = PrefixLoader({
+ 'app1': PackageLoader('mypackage.app1'),
+ 'app2': PackageLoader('mypackage.app2')
+ })
By loading ``'app1/index.html'`` the file from the app1 package is loaded,
by loading ``'app2/index.html'`` the file from the second.
>>> loader = ChoiceLoader([
... FileSystemLoader('/path/to/user/templates'),
- ... PackageLoader('mypackage')
+ ... FileSystemLoader('/path/to/system/templates')
... ])
This is useful if you want to allow users to override builtin templates
>>> tmpl = env.get_template('syntaxerror.html')
Traceback (most recent call last):
...
-TemplateSyntaxError: unknown tag 'endif'
- File "loaderres/templates\\syntaxerror.html", line 4
- {% endif %}
+ File "tests/loaderres/templates/syntaxerror.html", line 4, in template
+ {% endif %}
+TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.
'''
:license: BSD, see LICENSE for more details.
"""
+import os
import time
import tempfile
from jinja2 import Environment, loaders
'justdict.html': 'FOO'
})
package_loader = loaders.PackageLoader('loaderres', 'templates')
-filesystem_loader = loaders.FileSystemLoader('loaderres/templates')
+filesystem_loader = loaders.FileSystemLoader('tests/loaderres/templates')
function_loader = loaders.FunctionLoader({'justfunction.html': 'FOO'}.get)
choice_loader = loaders.ChoiceLoader([dict_loader, package_loader])
prefix_loader = loaders.PrefixLoader({