[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
Provenance
- Commit
2165ce6- Tree
0963aa9- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ccdc022
4 files changed+108 added−10 removed
modified:docs/src/loaders.txt+99 added-4 removed
13 unmodified lines14 15 Loader Baseclasses16 ==================17 18-With Jinja 1.1 onwards all the loaders have (except of the uncached) now have19-baseclasses you can use to mix your own caching layer in. This technique is20-explained below. The `BaseLoader` itself is also a loader baseclass but because21-it's the baseclass of each loader it's explained in detail below.18+With Jinja 1.1 onwards all the loaders have (except of the uncached)19+baseclasses. You can use them to mix your own caching layer in. This technique20+is described below. The `BaseLoader` itself is also a loader baseclass but21+because it's the baseclass of all loaders it's covered in the "Developing22+Loaders" section.23 24 [[list_of_baseloaders]]25 26 Developing Loaders117 unmodified lines144 'template/' # string prefix for the cache keys145 )146 147 .. _memcached: http://www.danga.com/memcached/148+149+How Mixin Classes Work150+======================151+152+The idea of the cached loader mixins is that you override the `load`153+method of the other base class so that it's only called to get the data154+from the loader and put it into a cache and then bypass the original `load`.155+156+This works because mixin classes, as well as the loaders are so called "new157+style classes" with a MRO (method resolution order). So it's possible to158+access the parent without actually knowing the name of it.159+160+Here as small mixin class that stores everything after loading in a161+dict:162+163+.. sourcecode:: python164+165+ class SimpleCacheMixin(object):166+167+ def __init__(self):168+ self.__cache = {}169+170+ 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 tmpl177+178+You can then mix the class in like any other mixin class. Note that179+all non public attributes **must** be prefixed with two underscores to180+enable the name mangling. Otherwise the mixin class could break the181+internal structure of the loader.182+183+The ``super(SimpleCacheMixin, self)`` call returns an object that looks184+up all the attributes you request in all the parent classes. The185+`SimpleCacheMixin` just has the `object` parent which makes it a new186+style 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 resulting188+class. Here a full example.189+190+Combining Everything191+====================192+193+Here a full example with a custom cache mixin and a custom base loader:194+195+.. sourcecode:: python196+197+ import codecs198+ from os.path import join199+ from jinja.loaders import BaseLoader200+ from jinja.exceptions import TemplateNotFound201+202+ class SimpleBaseLoader(BaseLoader):203+ 204+ def __init__(self, path):205+ self.path = path206+207+ 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()216+217+218+ class SimpleCacheMixin(object):219+220+ def __init__(self):221+ self.__cache = {}222+223+ 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 tmpl230+231+232+ class SimpleLoader(SimpleBaseLoader, SimpleCacheMixin):233+234+ def __init__(self, path):235+ SimpleBaseLoader.__init__(self, path)236+ SimpleCacheMixin.__init__()237+238+You can of course put all the functionallity into the `SimpleLoader` but then239+you cannot exchange parts of it without rewriting much code. In the example240+above replacing the `SimpleCacheMixin` with a `MemcachedLoaderMixin` is a241+matter of 20 seconds.modified:jinja/environment.py+8 added-5 removed
127 unmodified lines128 # 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_traceback142 143 # 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 = namespace147 148 # jinja 1.0 compatibility149 if auto_escape:150 self.default_filters.append(('escape', (True,)))207 unmodified linesadded:MANIFEST.in+1 added
1+include docs/build/*.htmlmodified:setup.py-1 removed
71 unmodified lines72 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