From 9a32e491011cfd677d9949275dcc4ece4dd1be9e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 15 Mar 2007 15:08:47 +0100 Subject: [PATCH] [svn] working on the goddamn baker plugin... --HG-- branch : trunk --- docs/src/frameworks.txt | 32 +++++++++++++++++++---- jinja/bakerplugin.py | 58 +++++++---------------------------------- 2 files changed, 37 insertions(+), 53 deletions(-) diff --git a/docs/src/frameworks.txt b/docs/src/frameworks.txt index 5423966..5efd7f0 100644 --- a/docs/src/frameworks.txt +++ b/docs/src/frameworks.txt @@ -28,6 +28,16 @@ something like this: 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 ---------- @@ -42,12 +52,24 @@ provide the jinja options: [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/ diff --git a/jinja/bakerplugin.py b/jinja/bakerplugin.py index 422e47e..81edeb9 100644 --- a/jinja/bakerplugin.py +++ b/jinja/bakerplugin.py @@ -12,7 +12,7 @@ from jinja import Environment -class ConfigurationError(Exception): +class ConfigurationError(ValueError): """ Raised if an configuration error occoured. """ @@ -27,55 +27,17 @@ class JinjaPlugin(object): 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): """ @@ -86,7 +48,7 @@ class JinjaPlugin(object): 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) -- 2.26.2