Skip to content
Myr

[svn] working on the goddamn baker plugin...

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 c97aab3 · Browse files · 2 files+37 added−53 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+37 added−53 removed

  • other:+37 added−53 removed

2 files+37 added−53 removed

modified:docs/src/frameworks.txt+27 added-5 removed
26 unmodified lines
2728 config.init_app(global_conf, app_conf, package='yourproject',29 template_engine='jinja')3031If you load templates using dotted notation the ``'.html'`` to the filename.32You can override this using ``jinja.exception``. eg:3334.. sourcecode:: python3536 config.add_template_engine('jinja', {37 'jinja.environment': ...,38 'jinja.extension': 'tmpl'39 })404142TurboGears43----------4445Integrating Jinja into TurboGears works pretty much the same.
5 unmodified lines
51.. sourcecode:: ini5253 [global]54 tg.defaultview = 'jinja'45 jinja.loader.searchpath = '/path/to/templates'55 jinja.init_callback = yourapplication.yourmodule.setup_function5647Because of the limitations of the configuration file there is currently no way48to configure jinja correctly. This problem does not exist with pylons since you49can pass any python object to the baker configuration system. We're currently50looking for solutions to this problem.57Now you have to edit the file `yourapplication.yourmodule` and add a58`setup_function` callback that creates an environment:5960.. sourcecode:: python6162 from jinja import Environment, FileSystemLoader6364 def setup_function(options):65 return Environment(loader=FileSystemLoader('path/to/templates'))6667This solution isn't the best but currently the only thing you can do. There is68a discussion about improving the plugin interface so that this can be solved69in a more elegant way.7071Also here exists the same limitation regarding dotted notation, see the72snipped and information in the pylons section.737475.. _TurboGears: http://www.turbogears.org/76.. _pylons: http://www.pylonshq.com/
modified:jinja/bakerplugin.py+10 added-48 removed
10 unmodified lines
11"""12from jinja import Environment131415class ConfigurationError(Exception):15class ConfigurationError(ValueError):16 """17 Raised if an configuration error occoured.18 """19
6 unmodified lines
2627 def __init__(self, extra_vars_func=None, options=None):28 self.get_extra_vars = extra_vars_func29 options = options or {}30 self.extension = options.get('jinja.extension', JinjaPlugin.extension)30 if 'jinja.environment' in options:31 self.environment = options['jinja.environment']32 elif 'jinja.init_callback' in options:33 name = options['jinja.init_callback']34 p = name.rsplit('.', 1)35 func = getattr(__import__(p[0], '', '', ['']), p[1])36 self.environment = func(options)37 else:34 # this wonderful piece of code was brought to you by the turbogears35 # ppl who want to put template configuration stuff into goddamn36 # text/plain configuration files.37 if 'jinja.environment.loader' in options:38 loader = options['jinja.environment.loader']39 else:40 loadername = options.get('jinja.loader') or 'FileSystemLoader'41 if '.' in loadername:42 p = loadername.rsplit('.', 1)43 loadercls = getattr(__import__(p[0], '', '', ['']), p[1])44 else:45 from jinja import loaders46 loadercls = getattr(loaders, loadername)47 loaderoptions = {}48 for k, v in options.iteritems():49 if k.startswith('jinja.loader.'):50 loaderoptions[k[14:]] = v51 loader = loadercls(**loaderoptions)52 if 'jinja.environment.context_class' in options:53 context_class = options['jinja.environment.context_class']54 else:55 contextname = options.get('jinja.context_class') or \56 'jinja.datastructure.Context'57 if '.' in contextname:58 p = contextname.rsplit('.', 1)59 context_class = getattr(__import__(p[0], '', '', ['']), p[1])60 else:61 from jinja import Context as context_class62 self.environment = Environment(63 block_start_string=options.get('jinja.block_start_string', '{%'),64 block_end_string=options.get('jinja.block_end_string', '%}'),65 variable_start_string=options.get('jinja.variable_start_string', '{{'),66 variable_end_string=options.get('jinja.variable_end_string', '}}'),67 comment_start_string=options.get('jinja.comment_start_string', '{#'),68 comment_end_string=options.get('jinja.comment_end_string', '#}'),69 trim_blocks=str(options.get('jinja.trim_blocks')).lower() in70 ('true', 'on', 'yes', '1'),71 template_charset=options.get('jinja.template_charset', 'utf-8'),72 charset=options.get('jinja.charset', 'utf-8'),73 namespace=options.get('jinja.namespace'),74 loader=loader,75 filters=options.get('jinja.filters'),76 tests=options.get('jinja.tests'),77 context_class=context_class78 )38 raise ConfigurationError('no jinja environment defined')39 if 'jinja.extension' in options:40 self.extension = options['jinja.extension']4142 def load_template(self, templatename, template_string=None):43 """44 Find a template specified in python 'dot' notation, or load one from
2 unmodified lines
47 if template_string is not None:48 return self.environment.from_string(template_string)4950 # Translate TG dot notation to normal / template path89 if '/' not in templatename and '.' not in templatename:51 if '/' not in templatename and '.' in templatename:52 templatename = '/' + templatename.replace('.', '/') + '.' + self.extension5354 return self.environment.get_template(templatename)55
11 unmodified lines