Skip to content
Myr

[svn] made jinja platform depending and adedd manifest for the built documentation. Improved loader documentation

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 ccdc022 · Browse files · 4 files+108 added−10 removed
This commit

State

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

Diff stats

4 files+108 added−10 removed

  • other:+108 added−10 removed

4 files+108 added−10 removed

modified:docs/src/loaders.txt+99 added-4 removed
13 unmodified lines
1415Loader Baseclasses16==================1718With Jinja 1.1 onwards all the loaders have (except of the uncached) now have19baseclasses you can use to mix your own caching layer in. This technique is20explained below. The `BaseLoader` itself is also a loader baseclass but because21it's the baseclass of each loader it's explained in detail below.18With Jinja 1.1 onwards all the loaders have (except of the uncached)19baseclasses. You can use them to mix your own caching layer in. This technique20is described below. The `BaseLoader` itself is also a loader baseclass but21because it's the baseclass of all loaders it's covered in the "Developing22Loaders" section.2324[[list_of_baseloaders]]2526Developing Loaders
117 unmodified lines
144 'template/' # string prefix for the cache keys145 )146147.. _memcached: http://www.danga.com/memcached/148149How Mixin Classes Work150======================151152The idea of the cached loader mixins is that you override the `load`153method of the other base class so that it's only called to get the data154from the loader and put it into a cache and then bypass the original `load`.155156This works because mixin classes, as well as the loaders are so called "new157style classes" with a MRO (method resolution order). So it's possible to158access the parent without actually knowing the name of it.159160Here as small mixin class that stores everything after loading in a161dict:162163.. sourcecode:: python164165 class SimpleCacheMixin(object):166167 def __init__(self):168 self.__cache = {}169170 def load(self, environment, name, translator):171 if name in self.__cache:172 return self.__cache[name]173 tmpl = super(SimpleCacheMixin, self).load(environment, name,174 translator)175 self.__cache[name] = tmpl176 return tmpl177178You can then mix the class in like any other mixin class. Note that179all non public attributes **must** be prefixed with two underscores to180enable the name mangling. Otherwise the mixin class could break the181internal structure of the loader.182183The ``super(SimpleCacheMixin, self)`` call returns an object that looks184up all the attributes you request in all the parent classes. The185`SimpleCacheMixin` just has the `object` parent which makes it a new186style class, but as soon as a loader is mixed in it will call the187`load` method of the loader that is the other parent of the resulting188class. Here a full example.189190Combining Everything191====================192193Here a full example with a custom cache mixin and a custom base loader:194195.. sourcecode:: python196197 import codecs198 from os.path import join199 from jinja.loaders import BaseLoader200 from jinja.exceptions import TemplateNotFound201202 class SimpleBaseLoader(BaseLoader):203 204 def __init__(self, path):205 self.path = path206207 def get_source(self, environment, name, parent):208 filename = join(self.path, name)209 if not path.exists(filename):210 raise TemplateNotFound(name)211 f = codecs.open(filename, 'r', environment.template_charset)212 try:213 return f.read()214 finally:215 f.close()216217218 class SimpleCacheMixin(object):219220 def __init__(self):221 self.__cache = {}222223 def load(self, environment, name, translator):224 if name in self.__cache:225 return self.__cache[name]226 tmpl = super(SimpleCacheMixin, self).load(environment, name,227 translator)228 self.__cache[name] = tmpl229 return tmpl230231232 class SimpleLoader(SimpleBaseLoader, SimpleCacheMixin):233234 def __init__(self, path):235 SimpleBaseLoader.__init__(self, path)236 SimpleCacheMixin.__init__()237238You can of course put all the functionallity into the `SimpleLoader` but then239you cannot exchange parts of it without rewriting much code. In the example240above replacing the `SimpleCacheMixin` with a `MemcachedLoaderMixin` is a241matter of 20 seconds.
modified:jinja/environment.py+8 added-5 removed
127 unmodified lines
128 # other stuff129 self.template_charset = template_charset130 self.charset = charset131 self.loader = loader132 self.filters = filters is None and DEFAULT_FILTERS.copy() or filters133 self.tests = tests is None and DEFAULT_TESTS.copy() or tests132 if filters is None:133 filters = DEFAULT_FILTERS.copy()134 self.filters = filters135 if tests is None:136 tests = DEFAULT_TESTS.copy()137 self.tests = tests138 self.default_filters = default_filters or []139 self.context_class = context_class140 self.undefined_singleton = undefined_singleton141 self.friendly_traceback = friendly_traceback142143 # global namespace144 if namespace is None:141 self.globals = DEFAULT_NAMESPACE.copy()142 else:143 self.globals = namespace145 namespace = DEFAULT_NAMESPACE.copy()146 self.globals = namespace147148 # jinja 1.0 compatibility149 if auto_escape:150 self.default_filters.append(('escape', (True,)))
207 unmodified lines
added:MANIFEST.in+1 added
1include docs/build/*.html
modified:setup.py-1 removed
71 unmodified lines
72 data_files = [73 ('docs', list(list_files('docs/build'))),74 ('docs/txt', list(list_files('docs/src')))75 ],76 platforms = 'any',76 entry_points='''77 [python.templating.engines]78 jinja = jinja.plugin:BuffetPlugin79 ''',
10 unmodified lines