config.init_app(global_conf, app_conf, package='yourproject',
template_engine='jinja')
+If you load templates using dotted notation the ``'.html'`` to the filename.
+You can override this using ``jinja.exception``. eg:
+
+.. sourcecode:: python
+
+ config.add_template_engine('jinja', {
+ 'jinja.environment': ...,
+ 'jinja.extension': 'tmpl'
+ })
+
TurboGears
----------
[global]
tg.defaultview = 'jinja'
- jinja.loader.searchpath = '/path/to/templates'
+ jinja.init_callback = yourapplication.yourmodule.setup_function
+
+Now you have to edit the file `yourapplication.yourmodule` and add a
+`setup_function` callback that creates an environment:
+
+.. sourcecode:: python
+
+ from jinja import Environment, FileSystemLoader
+
+ def setup_function(options):
+ return Environment(loader=FileSystemLoader('path/to/templates'))
+
+This solution isn't the best but currently the only thing you can do. There is
+a discussion about improving the plugin interface so that this can be solved
+in a more elegant way.
-Because of the limitations of the configuration file there is currently no way
-to configure jinja correctly. This problem does not exist with pylons since you
-can pass any python object to the baker configuration system. We're currently
-looking for solutions to this problem.
+Also here exists the same limitation regarding dotted notation, see the
+snipped and information in the pylons section.
.. _TurboGears: http://www.turbogears.org/
from jinja import Environment
-class ConfigurationError(Exception):
+class ConfigurationError(ValueError):
"""
Raised if an configuration error occoured.
"""
def __init__(self, extra_vars_func=None, options=None):
self.get_extra_vars = extra_vars_func
options = options or {}
- self.extension = options.get('jinja.extension', JinjaPlugin.extension)
if 'jinja.environment' in options:
self.environment = options['jinja.environment']
+ elif 'jinja.init_callback' in options:
+ name = options['jinja.init_callback']
+ p = name.rsplit('.', 1)
+ func = getattr(__import__(p[0], '', '', ['']), p[1])
+ self.environment = func(options)
else:
- # this wonderful piece of code was brought to you by the turbogears
- # ppl who want to put template configuration stuff into goddamn
- # text/plain configuration files.
- if 'jinja.environment.loader' in options:
- loader = options['jinja.environment.loader']
- else:
- loadername = options.get('jinja.loader') or 'FileSystemLoader'
- if '.' in loadername:
- p = loadername.rsplit('.', 1)
- loadercls = getattr(__import__(p[0], '', '', ['']), p[1])
- else:
- from jinja import loaders
- loadercls = getattr(loaders, loadername)
- loaderoptions = {}
- for k, v in options.iteritems():
- if k.startswith('jinja.loader.'):
- loaderoptions[k[14:]] = v
- loader = loadercls(**loaderoptions)
- if 'jinja.environment.context_class' in options:
- context_class = options['jinja.environment.context_class']
- else:
- contextname = options.get('jinja.context_class') or \
- 'jinja.datastructure.Context'
- if '.' in contextname:
- p = contextname.rsplit('.', 1)
- context_class = getattr(__import__(p[0], '', '', ['']), p[1])
- else:
- from jinja import Context as context_class
- self.environment = Environment(
- block_start_string=options.get('jinja.block_start_string', '{%'),
- block_end_string=options.get('jinja.block_end_string', '%}'),
- variable_start_string=options.get('jinja.variable_start_string', '{{'),
- variable_end_string=options.get('jinja.variable_end_string', '}}'),
- comment_start_string=options.get('jinja.comment_start_string', '{#'),
- comment_end_string=options.get('jinja.comment_end_string', '#}'),
- trim_blocks=str(options.get('jinja.trim_blocks')).lower() in
- ('true', 'on', 'yes', '1'),
- template_charset=options.get('jinja.template_charset', 'utf-8'),
- charset=options.get('jinja.charset', 'utf-8'),
- namespace=options.get('jinja.namespace'),
- loader=loader,
- filters=options.get('jinja.filters'),
- tests=options.get('jinja.tests'),
- context_class=context_class
- )
+ raise ConfigurationError('no jinja environment defined')
+ if 'jinja.extension' in options:
+ self.extension = options['jinja.extension']
def load_template(self, templatename, template_string=None):
"""
return self.environment.from_string(template_string)
# Translate TG dot notation to normal / template path
- if '/' not in templatename and '.' not in templatename:
+ if '/' not in templatename and '.' in templatename:
templatename = '/' + templatename.replace('.', '/') + '.' + self.extension
return self.environment.get_template(templatename)