Edgewall Software

source: trunk/doc/plugin.txt

Last change on this file was 895, checked in by cmlenz, 15 years ago

Includes from templates loaded via an absolute path now include the correct file in nested directories as long if no search path has been configured. Closes #240.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-rst
File size: 10.5 KB
RevLine 
[537]1.. -*- mode: rst; encoding: utf-8 -*-
2
3===========================
4Using the Templating Plugin
5===========================
6
7While you can easily use Genshi templating through the APIs provided directly
8by Genshi, in some situations you may want to use Genshi through the template
9engine plugin API. Note though that this considerably limits the power and
10flexibility of Genshi templates (for example, there's no good way to use filters
11such as Genshi's `HTMLFormFiller`_ when the plugin
12API is sitting between your code and Genshi).
13
14.. _`HTMLFormFiller`: filters.html>#html-form-filler
15
16
17.. contents:: Contents
18   :depth: 2
19.. sectnum::
20
21
22Introduction
23============
24
[895]25Some Python web frameworks support a variety of different templating engines
26through the `Template Engine Plugin API`_, which was first developed by the
27Buffet_ and TurboGears_ projects.
[537]28
29.. _`Template Engine Plugin API`: http://docs.turbogears.org/1.0/TemplatePlugins
30.. _`Buffet`: http://projects.dowski.com/projects/buffet
31.. _`TurboGears`: http://www.turbogears.org/
32
33Genshi supports this API out of the box, so you can use it in frameworks like
34TurboGears or `Pylons`_ without installing any additional packages. A small
35example TurboGears application is included in the ``examples`` directory of
36source distributions of Genshi.
37
38.. _`Pylons`: http://pylonshq.com/
39
40
41Usage
42=====
43
44The way you use Genshi through the plugin API depends very much on the framework
45you're using. In general, the approach will look something like the following:
46
47(1) Configure Genshi as the default (or an additional) template engine
48(2) Optionally specify Genshi-specific `configuration options`_
49(3) For any given *view* or *controller* (or whatever those are called in your
50    framework of choice), specify the name of the template to use and which data
51    should be made available to it.
52
53For point 1, you'll have to specify the *name* of the template engine plugin.
54For Genshi, this is **"genshi"**. However, because Genshi supports both markup
55and text templates, it also provides two separate plugins, namely
56**"genshi-markup"** and **"genshi-text"** (the "genshi" name is just an
57alias for "genshi-markup").
58
59Usually, you can choose a default template engine, but also use a different
60engine on a per-request basis. So to use markup templates in general, but a text
61template in a specific controller, you'd configure "genshi" as the default
62template engine, and specify "genshi-text" for the controllers that should use
63text templates. How exactly this works depends on the framework in use.
64
65When rendering a specific template in a controller (point 3 above), you may also
66be able to pass additional options to the plugin. This includes the ``format``
67keyword argument, which Genshi will use to override the configured default
68serialization method. In combination with specifying the "genshi-text" engine
69name as explained above, you would use this to specify the "text" serialization
70method when you want to use a text template. Or you'd specify "xml" for the
71format when you want to produce an Atom feed or other XML content.
72
73
[895]74Template Paths
75--------------
76
77How you specify template paths depends on whether you have a `search path`_ set
78up or not. The search path is a list of directories that Genshi should load
79templates from. Now when you request a template using a relative path such as
80``mytmpl.html`` or ``foo/mytmpl.html``, Genshi will look for that file in the
81directories on the search path.
82
83For mostly historical reasons, the Genshi template engine plugin uses a
84different approach when you **haven't** configured the template search path:
85you now load templates using *dotted notation*, for example ``mytmpl`` or
86``foo.mytmpl``.  Note how you've lost the ability to explicitly specify the
87file extension: you now have to use ``.html`` for markup templates, and
88``.txt`` for text templates.
89
90Using the search path is recommended for a number of reasons: First, it's
91the native Genshi model and is thus more robust and better supported.
92Second, a search path gives you much more flexibility for organizing your
93application templates. And as noted above, you aren't forced to use hardcoded
94filename extensions for your template files.
95
96
[537]97Extra Implicit Objects
[895]98----------------------
[537]99
100The "genshi-markup" template engine plugin adds some extra functions that are
101made available to all templates implicitly, namely:
102
103``HTML(string)``
104  Parses the given string as HTML and returns a markup stream.
105``XML(string)``
106  Parses the given string as XML and returns a markup stream.
107``ET(tree)``
108  Adapts the given `ElementTree`_ object to a markup stream.
109
110The framework may make additional objects available by default. Consult the
111documentation of your framework for more information.
112
113.. _elementtree: http://effbot.org/zone/element-index.htm
114
115
116.. _`configuration options`:
117
118Configuration Options
119=====================
120
121The plugin API allows plugins to be configured using a dictionary of strings.
122The following is a list of configuration options that Genshi supports. These may
123or may not be made available by your framework. TurboGears 1.0, for example,
124only passes a fixed set of options to all plugins.
125
[654]126``genshi.allow_exec``
127--------------------------
128Whether the Python code blocks should be permitted in templates. Specify "yes"
129to allow code blocks (which is the default), or "no" otherwise. Please note
130that disallowing code blocks in templates does not turn Genshi into a
131sandboxable template engine; there are sufficient ways to do harm even using
132plain expressions.
133
[537]134``genshi.auto_reload``
135----------------------
136Whether the template loader should check the last modification time of template
137files, and automatically reload them if they have been changed. Specify "yes"
138to enable this reloading (which is the default), or "no" to turn it off.
139
[657]140You probably want to disable reloading in a production environment to improve
141performance of both templating loading and the processing of includes. But
142remember that you'll then have to manually restart the server process anytime
143the templates are updated.
[537]144
145``genshi.default_doctype``
146--------------------------
147The default ``DOCTYPE`` declaration to use in generated markup. Valid values
148are:
149
150**html-strict** (or just **html**)
151  HTML 4.01 Strict
152**html-transitional**
153  HTML 4.01 Transitional
154**xhtml-strict** (or just **xhtml**)
155  XHTML 1.0 Strict
156**xhtml-transitional**
157  XHTML 1.0 Transitional
[540]158**html5**
159  HTML5 (as `proposed`_ by the WHAT-WG)
[537]160
[540]161.. _proposed: http://www.whatwg.org/specs/web-apps/current-work/
162
[537]163.. note:: While using the Genshi API directly allows you to specify document
164          types not in that list, the *dictionary-of-strings* based
165          configuration utilized by the plugin API unfortunately limits your
166          choices to those listed above.
167
168The default behavior is to not do any prepending/replacing of a ``DOCTYPE``, but
169rather pass through those defined in the templates (if any). If this option is
170set, however, any ``DOCTYPE`` declarations in the templates are replaced by the
171specified document type.
172
173Note that with (X)HTML, the presence and choice of the ``DOCTYPE`` can have a
174more or less dramatic impact on how modern browsers render pages that use CSS
175style sheets. In particular, browsers may switch to *quirks rendering mode* for
176certain document types, or when the ``DOCTYPE`` declaration is missing
177completely.
178
179For more information on the choice of the appropriate ``DOCTYPE``, see:
180
181* `Recommended DTDs to use in your Web document <http://www.w3.org/QA/2002/04/valid-dtd-list.html>`_
182* `Choosing a DOCTYPE <http://htmlhelp.com/tools/validator/doctype.html>`_
183
184``genshi.default_encoding``
185---------------------------
186The default output encoding to use when serializing a template. By default,
187Genshi uses UTF-8. If you need to, you can choose a different charset by
188specifying this option, although that rarely makes sense.
189
190As Genshi is not in control over what HTTP headers are being sent together with
191the template output, make sure that you (or the framework you're using)
192specify the chosen encoding as part of the outgoing ``Content-Type`` header.
193For example::
194
195  Content-Type: text/html; charset=utf-8
196
197.. note:: Browsers commonly use ISO-8859-1 by default for ``text/html``, so even
198          if you use Genshi's default UTF-8 encoding, you'll have to let the
199          browser know about that explicitly
200
201``genshi.default_format``
202-------------------------
203Determines the default serialization method to use. Valid options are:
204
205**xml**
206  Serialization to XML
207**xhtml**
208  Serialization to XHTML in a way that should be compatible with HTML (i.e. the
209  result can be sent using the ``text/html`` MIME type, but can also be handled
210  by XML parsers if you're careful).
211**html**
212  Serialization to HTML
213**text**
214  Plain text serialization
215
216See `Understanding HTML, XML and XHTML`_ for an excellent description of the
217subtle differences between the three different markup serialization options. As
218a general recommendation, if you don't have a special requirement to produce
219well-formed XML, you should probably use the **html** option for your web sites.
220
221.. _`Understanding HTML, XML and XHTML`: http://webkit.org/blog/?p=68
222
[646]223``genshi.loader_callback``
224--------------------------
225The callback function that should be invoked whenever the template loader loads
226a new template.
227
228.. note:: Unlike the other options, this option can **not** be passed as
229          a string value, but rather must be a reference to the actual function.
230          That effectively means it can not be set from (non-Python)
231          configuration files.
232
[537]233``genshi.lookup_errors``
234------------------------
235The error handling style to use in template expressions. Can be either
236**lenient** (the default) or **strict**. See the `Error Handling`_ section for
237detailled information on the differences between these two modes.
238
239.. _`Error Handling`: templates.html#template-expressions-and-code-blocks
240
241``genshi.max_cache_size``
242-------------------------
243The maximum number of templates that the template loader will cache in memory.
244The default value is **25**. You may want to choose a higher value if your web
245site uses a larger number of templates, and you have enough memory to spare.
246
[706]247``genshi.new_text_syntax``
248--------------------------
249Whether the new syntax for text templates should be used. Specify "yes" to
250enable the new syntax, or "no" to use the old syntax.
251
252In the version of Genshi, the default is to use the old syntax for
253backwards-compatibility, but that will change in a future release.
254
[895]255.. _`search path`:
256
[537]257``genshi.search_path``
258----------------------
259A colon-separated list of file-system path names that the template loader should
260use to search for templates.
Note: See TracBrowser for help on using the repository browser.