annotate doc/loader.txt @ 883:0849dcc8ed46

Added a documentation page about the template loader.
author cmlenz
date Fri, 16 Apr 2010 19:58:25 +0000
parents
children 5a1c0ee0f659
rev   line source
883
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
2
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
3 =================
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
4 Loading Templates
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
5 =================
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
6
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
7 Genshi comes with a simple but flexible implementation of a template loader in
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
8 the ``genshi.template.loader`` module. The loader provides caching of
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
9 templates so they do not need to be reparsed when used, support for multiple
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
10 template directories that together form a virtual search path, as well as
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
11 support for different template loading strategies.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
12
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
13 .. contents:: Contents
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
14 :depth: 3
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
15 .. sectnum::
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
16
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
17
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
18 -----
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
19 Usage
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
20 -----
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
21
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
22 The basic usage pattern is simple: instantiate one ``TemplateLoader`` object
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
23 and keep it around, then ask it to load a template whenever you need to load
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
24 one:
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
25
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
26 .. code-block:: pycon
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
27
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
28 >>> from genshi.template import TemplateLoader
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
29
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
30 >>> loader = TemplateLoader(['/path/to/dir1', '/path/to/dir2'],
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
31 ... auto_reload=True)
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
32 >>> loader.load('test.html')
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
33 <MarkupTemplate "test.html">
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
34
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
35 When you try to load a template that can't be found, you get a
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
36 ``TemplateNotFound`` error:
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
37
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
38 .. code-block:: pycon
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
39
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
40 >>> loader.load('foobar.html')
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
41 Traceback (most recent call last):
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
42 ...
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
43 TemplateNotFound: Template "foobar.html" not found
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
44
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
45 The default template class used by the loader is ``MarkupTemplate``, but that
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
46 can be overridden both with a different default (as a keyword argument to the
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
47 ``TemplateLoader`` constructor), as well as for every individual invocation of
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
48 the ``load()`` method:
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
49
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
50 .. code-block:: pycon
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
51
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
52 >>> from genshi.template.text import NewTextTemplate
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
53 >>> loader.load('mail.txt', cls=NewTextTemplate)
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
54 <NewTextTemplate "mail.txt">
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
55
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
56
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
57 -------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
58 Caching
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
59 -------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
60
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
61 The ``TemplateLoader`` class provides a simple in-memory cache for parsed
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
62 template objects. This improves performance, because templates do not need to
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
63 be reparsed every time they are rendered.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
64
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
65 The size of this cache can be adjusted using the `max_cache_size` option on
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
66 the ``TemplateLoader`` constructor. The value of that option determines the
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
67 maximum number of template objects kept in the cache. When this limit is
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
68 reached, any templates that haven't been used in a while get purged.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
69 Technically, this is a least-recently-used (LRU) cache.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
70
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
71 Once a template has been cached, it will normally not get reparsed until it
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
72 has been purged from the cache. This means that any changes to the template
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
73 files are not taken into consideration as long as it is still found in the
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
74 cache. As this is inconvenient in development scenarios, the ``auto_reload``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
75 option allows for automatic cache invalidation based on whether the template
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
76 source has changed. But in production environments, automatic reloading should
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
77 be disabled, as it does affect performance negatively.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
78
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
79
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
80 --------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
81 Template Search Path
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
82 --------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
83
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
84 The template loader can be configured with a list of multiple directories in
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
85 which to search for templates. Taken together, these directories are mapped to
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
86 a single logical directory for locating templates by file name.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
87
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
88 The order of the directories making up the search path is significant: the
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
89 loader will first try to locate a requested template in the first directory,
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
90 then in the second, and so on. If there are two templates with the same file
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
91 name in multiple directories on the search path, the file found first is used.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
92
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
93 Based on this design, an application could, for example, configure a search
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
94 path consisting of a directory containing the default templates, as well as a
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
95 directory where site-specific templates can be stored that will override the
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
96 default templates.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
97
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
98
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
99 Load Functions
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
100 ==============
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
101
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
102 Usually the search path consists of strings representing directory paths, but
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
103 it may also contain “load functions”: functions that are basically invoked
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
104 with the file name, and return the template content.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
105
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
106 Genshi comes with three builtin load functions:
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
107
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
108 ``directory(path)``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
109 -------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
110
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
111 The equivalent of just using a string containing the directory path: looks up
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
112 the file name in a specific directory.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
113
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
114 .. code-block:: pycon
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
115
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
116 >>> from genshi.template import TemplateLoader, loader
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
117 >>> tl = TemplateLoader([loader.directory('/path/to/dir/')])
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
118
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
119 That is the same as:
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
120
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
121 .. code-block:: pycon
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
122
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
123 >>> tl = TemplateLoader(['/path/to/dir/'])
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
124
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
125
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
126 ``package(name, path)``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
127 -----------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
128
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
129 Uses the ``pkg_resources`` API to locate files in Python package data (which
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
130 may be inside a ZIP archive).
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
131
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
132 .. code-block:: pycon
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
133
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
134 >>> from genshi.template import TemplateLoader, loader
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
135 >>> tl = TemplateLoader([loader.package('myapp', 'templates')])
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
136
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
137 This will look for templates in the ``templates`` directory of the Python
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
138 package ``myapp``.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
139
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
140 ``prefixed(**delegates)``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
141 -------------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
142
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
143 Delegates load requests to different load functions based on the path prefix.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
144
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
145 .. code-block:: pycon
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
146
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
147 >>> from genshi.template import TemplateLoader, loader
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
148 >>> tl = TemplateLoader(loader.prefixed(
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
149 ... core = '/tmp/dir1',
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
150 ... plugin1 = loader.package('plugin1', 'templates'),
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
151 ... plugin2 = loader.package('plugin2', 'templates'),
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
152 ... ))
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
153 >>> tl.load('core/index.html')
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
154 <MarkupTemplate "core/index.html">
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
155
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
156 This example sets up a loader with three delegates, under the prefixes “core”,
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
157 “plugin1”, and “plugin2”. When a template is requested the ``prefixed`` load
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
158 function looks for a delegate with a corresponding prefix, removes the prefix
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
159 from the path and asks the delegate to load the template.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
160
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
161 In this case, assuming the directory ``/path/to/dir`` contains a file named
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
162 ``index.html``, that file will be used when we load ``core/index.html``. The
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
163 other delegate are not checked as their prefix does not match.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
164
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
165
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
166 .. note:: These builtin load functions are available both as class methods
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
167 of the ``TemplateLoader`` class as well as on the module level
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
168
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
169
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
170 Custom Load Functions
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
171 ---------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
172
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
173 You can easily use your own load function with the template loader, for
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
174 example to load templates from a database. All that is needed is a callable
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
175 object that accepts a ``filename`` (a string) and returns a tuple of the form
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
176 ``(filepath, filename, fileobj, uptodate_fun)``, where:
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
177
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
178 ``filepath``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
179 is the absolute path to the template
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
180 ``filename``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
181 is the base name of the template file
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
182 ``fileobj``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
183 is a readable file-like object that provides the content of the template
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
184 ``uptodate_fun``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
185 is a function that the loader can invoke to check whether the cached version
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
186 of the template is still up-to-date, or ``None`` if the load function is not
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
187 able to provide such a check. If provided, the function should not expect
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
188 any parameters (so you'll definitely want to use a closure here), and should
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
189 return ``True`` if the template has not changed since it was last loaded.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
190
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
191 When the requested template can not be found, the function should raise an
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
192 ``IOError`` or ``TemplateNotFound`` exception.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
193
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
194
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
195 ------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
196 Customized Loading
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
197 ------------------
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
198
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
199 If you require a completely different implementation of template loading, you
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
200 can extend or even replace the builtin ``TemplateLoader`` class.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
201
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
202 Protocol
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
203 ========
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
204
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
205 The protocol between the template loader and the templates itself is simple
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
206 and only used for processing includes. The only required part of that protocol
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
207 is that the object assigned to ``Template.loader`` implements a ``load``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
208 method compatible to that of the ``TemplateLoader`` class, at the minimum with
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
209 the signature ``load(filename, relative_to=None, cls=None)``.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
210
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
211 In addition, templates currently check for the existence and value of a boolean
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
212 ``auto_reload`` property. If the property exists and evaluates to a non-truth
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
213 value, inlining of included templates is disabled. Inlining is a small
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
214 optimization that removes some overhead in the processing of includes.
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
215
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
216 Subclassing ``TemplateLoader``
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
217 ==============================
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
218
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
219 You can also adjust the behavior of the ``TemplateLoader`` class by subclassing
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
220 it. You can of course override anything needed, but the class also provides the
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
221 ``_instantiate()`` hook, which is intended for use by subclasses to customize
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
222 the creation of the template object from the file name and content. Please
0849dcc8ed46 Added a documentation page about the template loader.
cmlenz
parents:
diff changeset
223 consult the code and the API documentation for more detail.
Copyright (C) 2012-2017 Edgewall Software