Skip to content
Myr

[svn] added jinja gti 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 0b51462 · Browse files · 1 file+87 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+87 added−0 removed

  • other:+87 added−0 removed

1 file+87 added−0 removed

added:jinja/plugin.py+87 added
1# -*- coding: utf-8 -*-2"""3 jinja.plugin4 ~~~~~~~~~~~~56 Support for the `GeneralTemplateInterface`__.78 __ http://trac.pocoo.org/wiki/GeneralTemplateInterface910 :copyright: 2007 by Armin Ronacher.11 :license: BSD, see LICENSE for more details.12"""13from jinja.environment import Environment14from jinja.loaders import FunctionLoader15from jinja.exceptions import TemplateNotFound161718def jinja_plugin_factory(options):19 """20 Basic implementation of the `GeneralTemplateInterface`.2122 Supports ``loader_func`` and ``getmtime_func``, as well as23 string and file loading but ignores ``mode`` since it's a24 text based template engine.2526 All options passed to this function are forwarded to the27 jinja environment. Exceptions are the following keys:2829 =================== =================================================30 ``environment`` If this is provided it must be the only31 configuration value and it's used as jinja32 environment.33 ``loader_func`` Function that takes the name of the template to34 load. If it returns a string or unicode object35 it's used to load a template. If the return36 value is None it's considered missing.37 ``getmtime_func`` Function used to check if templates requires38 reloading. Has to return the UNIX timestamp of39 the last template change or 0 if this template40 does not exist or requires updates at any cost.41 ``use_memcache`` Set this to ``True`` to enable memory caching.42 This is usually a good idea in production mode,43 but disable it during development since it won't44 reload template changes automatically.45 This only works in persistent environments like46 FastCGI.47 ``memcache_size`` Number of template instance you want to cache.48 Defaults to ``40``.49 ``cache_folder`` Set this to an existing directory to enable50 caching of templates on the file system. Note51 that this only affects templates transformed52 into python code. Default is ``None`` which means53 that caching is disabled.54 ``auto_reload`` Set this to `False` for a slightly better55 performance. In that case of `getmtime_func`56 not being provided this won't have an effect.57 =================== =================================================58 """59 if 'environment' in options:60 env = options['environment']61 if not len(options) == 1:62 raise TypeError('if environment provided no other '63 'arguments are allowed')64 else:65 loader_func = options.pop('loader_func', None)66 getmtime_func = options.pop('getmtime_func', None)67 use_memcache = options.pop('use_memcache', False)68 memcache_size = options.pop('memcache_size', 40)69 cache_folder = options.pop('cache_folder', None)70 auto_reload = options.pop('auto_reload', True)71 if loader_func is not None:72 options['loader'] = FunctionLoader(loader_func, getmtime_func,73 use_memcache, memcache_size,74 cache_folder, auto_reload)75 env = Environment(**options)7677 def render_function(template, values, options):78 if options.get('is_string'):79 tmpl = env.from_string(template)80 else:81 try:82 tmpl = env.get_template(template)83 except TemplateNotFound:84 return85 return (tmpl.render(**values),)8687 return render_function