[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
Provenance
- Commit
9a32e49- Tree
edb6ac5- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c97aab3
2 files changed+37 added−53 removed
modified:docs/src/frameworks.txt+27 added-5 removed
26 unmodified lines27 28 config.init_app(global_conf, app_conf, package='yourproject',29 template_engine='jinja')30 31+If you load templates using dotted notation the ``'.html'`` to the filename.32+You can override this using ``jinja.exception``. eg:33 34+.. sourcecode:: python35+36+ config.add_template_engine('jinja', {37+ 'jinja.environment': ...,38+ 'jinja.extension': 'tmpl'39+ })40+41+42 TurboGears43 ----------44 45 Integrating Jinja into TurboGears works pretty much the same.5 unmodified lines51 .. sourcecode:: ini52 53 [global]54 tg.defaultview = 'jinja'45- jinja.loader.searchpath = '/path/to/templates'55+ jinja.init_callback = yourapplication.yourmodule.setup_function56 47-Because of the limitations of the configuration file there is currently no way48-to configure jinja correctly. This problem does not exist with pylons since you49-can pass any python object to the baker configuration system. We're currently50-looking for solutions to this problem.57+Now you have to edit the file `yourapplication.yourmodule` and add a58+`setup_function` callback that creates an environment:59 60+.. sourcecode:: python61 62+ from jinja import Environment, FileSystemLoader63+64+ def setup_function(options):65+ return Environment(loader=FileSystemLoader('path/to/templates'))66+67+This solution isn't the best but currently the only thing you can do. There is68+a discussion about improving the plugin interface so that this can be solved69+in a more elegant way.70+71+Also here exists the same limitation regarding dotted notation, see the72+snipped and information in the pylons section.73+74+75 .. _TurboGears: http://www.turbogears.org/76 .. _pylons: http://www.pylonshq.com/modified:jinja/bakerplugin.py+10 added-48 removed
10 unmodified lines11 """12 from jinja import Environment13 14 15-class ConfigurationError(Exception):15+class ConfigurationError(ValueError):16 """17 Raised if an configuration error occoured.18 """19 6 unmodified lines26 27 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']41 42 def load_template(self, templatename, template_string=None):43 """44 Find a template specified in python 'dot' notation, or load one from2 unmodified lines47 if template_string is not None:48 return self.environment.from_string(template_string)49 50 # 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.extension53 54 return self.environment.get_template(templatename)55 11 unmodified lines