annotate doc/loader.txt @ 1044:ddb9354ad555 stable-0.7.x

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