1 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3 <title>Twisted Documentation: HTML Templating with twisted.web.template</title>
4 <link href="stylesheet.css" rel="stylesheet" type="text/css"/>
8 <h1 class="title">HTML Templating with twisted.web.template</h1>
9 <div class="toc"><ol><li><a href="#auto0">A Very Quick Introduction To Templating In Python</a></li><li><a href="#auto1">twisted.web.template - Why And How you Might Want to Use It</a></li><ul><li><a href="#auto2">Template Attributes</a></li><li><a href="#auto3">Slots</a></li><li><a href="#auto4">Iteration</a></li><li><a href="#auto5">Sub-views</a></li><li><a href="#auto6">Transparent</a></li></ul><li><a href="#auto7">Quoting</a></li><li><a href="#auto8">Deferreds</a></li><li><a href="#auto9">A Brief Note on Formats and DOCTYPEs</a></li><li><a href="#auto10">A Bit of History</a></li></ol></div>
12 <h2>A Very Quick Introduction To Templating In Python<a name="auto0"/></h2>
14 HTML templating is the process of transforming a template document (one which
15 describes style and structure, but does not itself include any content) into
16 some HTML output which includes information about objects in your application.
17 There are many, many libraries for doing this in Python: to name a few, <a href="http://jinja.pocoo.org/" shape="rect">jinja2</a>, <a href="http://docs.djangoproject.com/en/dev/ref/templates/" shape="rect">django templates</a>,
18 and <a href="http://www.clearsilver.net/" shape="rect">clearsilver</a>. You can easily use
19 any of these libraries in your Twisted Web application, either by running them
20 as <a href="web-in-60/wsgi.html" shape="rect">WSGI applications</a> or by calling your
21 preferred templating system's APIs to produce their output as strings, and then
22 writing those strings to <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.request.Request.write.html" title="twisted.web.request.Request.write">Request.write</a></code>.
24 <p>Before we begin explaining how to use it, I'd like to stress that you
25 don't <i>need</i> to use Twisted's templating system if you prefer some other
26 way to generate HTML. Use it if it suits your personal style or your
27 application, but feel free to use other things. Twisted includes templating for
28 its own use, because the <code>twisted.web</code> server needs to produce HTML
29 in various places, and we didn't want to add another large dependency for that.
30 Twisted is <em>not</em> in any way incompatible with other systems, so that has
31 nothing to do with the fact that we use our own.</p>
34 <h2>twisted.web.template - Why And How you Might Want to Use It<a name="auto1"/></h2>
36 Twisted includes a templating system, <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.html" title="twisted.web.template">twisted.web.template</a></code>. This can be convenient for Twisted
37 applications that want to produce some basic HTML for a web interface without an
38 additional dependency.
41 <code>twisted.web.template</code> also includes
42 support for <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.defer.Deferred.html" title="twisted.internet.defer.Deferred">Deferred</a></code>s, so
43 you can incrementally render the output of a page based on the results of <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.defer.Deferred.html" title="twisted.internet.defer.Deferred">Deferred</a></code>s that your application
44 has returned. This feature is fairly unique among templating libraries.
47 In <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.html" title="twisted.web.template">twisted.web.template</a></code>, templates are XHTML files
48 which also contain a special namespace for indicating dynamic portions of the
49 document. For example:
51 <div class="html-listing"><pre class="htmlsource">
52 <html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
54 <div t:render="header" />
55 <div id="content">
56 <p>Content goes here.</p>
58 <div t:render="footer" />
61 </pre><div class="caption">template example - <a href="listings/template-1.xml"><span class="filename">listings/template-1.xml</span></a></div></div>
62 The basic unit of templating is <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.Element.html" title="twisted.web.template.Element">twisted.web.template.Element</a></code>. An Element is given a way of
63 loading a bit of markup like the above example, and knows how to
64 correlate <code>render</code> attributes within that markup to Python methods
65 exposed with <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.renderer.html" title="twisted.web.template.renderer">twisted.web.template.renderer</a></code>:
66 <div class="py-listing"><pre><p class="py-linenumber"> 1
79 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">XMLFile</span>
80 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
82 <span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleElement</span>(<span class="py-src-parameter">Element</span>):
83 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'template-1.xml'</span>))
85 @<span class="py-src-variable">renderer</span>
86 <span class="py-src-keyword">def</span> <span class="py-src-identifier">header</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
87 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-string">'Header.'</span>)
89 @<span class="py-src-variable">renderer</span>
90 <span class="py-src-keyword">def</span> <span class="py-src-identifier">footer</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
91 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-string">'Footer.'</span>)
92 </pre><div class="caption">element example - <a href="listings/element_1.py"><span class="filename">listings/element_1.py</span></a></div></div>
93 In order to combine the two, we must render the element. For this simple
94 example, we can use the <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.flattenString.html" title="twisted.web.template.flattenString">flattenString</a></code> API, which will convert a
95 single template object - such as an <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.Element.html" title="twisted.web.template.Element">Element</a></code> - into a <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.defer.Deferred.html" title="twisted.internet.defer.Deferred">Deferred</a></code> which fires with a single string,
96 the HTML output of the rendering process.
97 <div class="py-listing"><pre><p class="py-linenumber">1
102 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">flattenString</span>
103 <span class="py-src-keyword">from</span> <span class="py-src-variable">element_1</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">ExampleElement</span>
104 <span class="py-src-keyword">def</span> <span class="py-src-identifier">renderDone</span>(<span class="py-src-parameter">output</span>):
105 <span class="py-src-keyword">print</span> <span class="py-src-variable">output</span>
106 <span class="py-src-variable">flattenString</span>(<span class="py-src-variable">None</span>, <span class="py-src-variable">ExampleElement</span>()).<span class="py-src-variable">addCallback</span>(<span class="py-src-variable">renderDone</span>)
107 </pre><div class="caption">rendering snippet - <a href="listings/render_1.py"><span class="filename">listings/render_1.py</span></a></div></div>
108 <p>This short program cheats a little bit; we know that there are no <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.defer.Deferred.html" title="twisted.internet.defer.Deferred">Deferred</a></code>s in the template which
109 require the reactor to eventually fire; therefore, we can simply add a callback
110 which outputs the result. Also, none of the <code>renderer</code> functions
111 require the <code>request</code> object, so it's acceptable to
112 pass <code>None</code> through here. (The 'request' object here is used only to
113 relay information about the rendering process to each renderer, so you may
114 always use whatever object makes sense for your application. Note, however,
115 that renderers from library code may require an <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.iweb.IRequest.html" title="twisted.web.iweb.IRequest">IRequest</a></code>.)</p>
117 If you run it yourself, you can see that it produces the following output:
119 <div class="html-listing"><pre class="htmlsource">
122 <div>Header.</div>
123 <div id="content">
124 <p>Content goes here.</p>
126 <div>Footer.</div>
129 </pre><div class="caption">rendering output 1 - <a href="listings/output-1.html"><span class="filename">listings/output-1.html</span></a></div></div>
130 The third parameter to a renderer method is a <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.Tag.html" title="twisted.web.template.Tag">Tag</a></code> object which represents the XML element
131 with the <code>t:render</code> attribute in the template. Calling a <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.Tag.html" title="twisted.web.template.Tag">Tag</a></code> adds children to the element
132 in the DOM, which may be strings, more <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.Tag.html" title="twisted.web.template.Tag">Tag</a></code>s, or other renderables such as <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.Element.html" title="twisted.web.template.Element">Element</a></code>s.
133 For example, to make the header and footer bold:
134 <div class="py-listing"><pre><p class="py-linenumber"> 1
147 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">XMLFile</span>, <span class="py-src-variable">tags</span>
148 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
150 <span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleElement</span>(<span class="py-src-parameter">Element</span>):
151 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'template-1.xml'</span>))
153 @<span class="py-src-variable">renderer</span>
154 <span class="py-src-keyword">def</span> <span class="py-src-identifier">header</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
155 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-variable">tags</span>.<span class="py-src-variable">b</span>(<span class="py-src-string">'Header.'</span>))
157 @<span class="py-src-variable">renderer</span>
158 <span class="py-src-keyword">def</span> <span class="py-src-identifier">footer</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
159 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-variable">tags</span>.<span class="py-src-variable">b</span>(<span class="py-src-string">'Footer.'</span>))
160 </pre><div class="caption">tag manipulation example - <a href="listings/element_2.py"><span class="filename">listings/element_2.py</span></a></div></div>
162 Rendering this in a similar way to the first example would produce:
164 <div class="html-listing"><pre class="htmlsource">
167 <div><b>Header.</b></div>
168 <div id="content">
169 <p>Content goes here.</p>
171 <div><b>Footer.</b></div>
174 </pre><div class="caption">tag manipulation output - <a href="listings/output-2.html"><span class="filename">listings/output-2.html</span></a></div></div>
176 In addition to adding children, call syntax can be used to set attributes on a
177 tag. For example, to change the <code>id</code> on the <code>div</code> while
180 <div class="py-listing"><pre><p class="py-linenumber"> 1
193 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">XMLFile</span>, <span class="py-src-variable">tags</span>
194 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
196 <span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleElement</span>(<span class="py-src-parameter">Element</span>):
197 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'template-1.xml'</span>))
199 @<span class="py-src-variable">renderer</span>
200 <span class="py-src-keyword">def</span> <span class="py-src-identifier">header</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
201 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-variable">tags</span>.<span class="py-src-variable">p</span>(<span class="py-src-string">'Header.'</span>), <span class="py-src-variable">id</span>=<span class="py-src-string">'header'</span>)
203 @<span class="py-src-variable">renderer</span>
204 <span class="py-src-keyword">def</span> <span class="py-src-identifier">footer</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
205 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-variable">tags</span>.<span class="py-src-variable">p</span>(<span class="py-src-string">'Footer.'</span>), <span class="py-src-variable">id</span>=<span class="py-src-string">'footer'</span>)
206 </pre><div class="caption">attributes example - <a href="listings/element_3.py"><span class="filename">listings/element_3.py</span></a></div></div>
208 And this would produce the following page:
210 <div class="html-listing"><pre class="htmlsource">
213 <div id="header"><p>Header.</p></div>
214 <div id="content">
215 <p>Content goes here.</p>
217 <div id="footer"><p>Footer.</p></div>
220 </pre><div class="caption">attributes output - <a href="listings/output-3.html"><span class="filename">listings/output-3.html</span></a></div></div>
223 Calling a tag mutates it, it and returns the tag itself, so you can pass it
224 forward and call it multiple times if you have multiple children or attributes
225 to add to it. <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.html" title="twisted.web.template">twisted.web.template</a></code> also exposes some
226 convenient objects for building more complex markup structures from within
227 renderer methods in the <code>tags</code> object. In the examples above, we've
228 only used <code>tags.p</code> and <code>tags.b</code>, but there should be a <code>tags.x</code> for each <em>x</em> which is a valid HTML tag. There may be
229 some omissions, but if you find one, please feel free to file a bug.
232 <h3>Template Attributes<a name="auto2"/></h3>
234 <code>t:attr</code> tags allow you to set HTML attributes
235 (like <code>href</code> in an <code><a href="...</code>) on an enclosing
238 <h3>Slots<a name="auto3"/></h3>
240 <code>t:slot</code> tags allow you to specify "slots" which you can
241 conveniently fill with multiple pieces of data straight from your Python
244 The following example demonstrates both <code>t:attr</code>
245 and <code>t:slot</code> in action. Here we have a layout which displays a person's
246 profile on your snazzy new Twisted-powered social networking site. We use
247 the <code>t:attr</code> tag to drop in the "src" attribute on the profile picture,
248 where the actual value of src attribute gets specified by a <code>t:slot</code>
249 tag <em>within</em> the <code>t:attr</code> tag. Confused? It should make more
250 sense when you see the code:
252 <div class="html-listing"><pre class="htmlsource">
253 <div xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
254 t:render="person_profile"
255 class="profile">
256 <img><t:attr name="src"><t:slot name="profile_image_url" /></t:attr></img>
257 <p><t:slot name="person_name" /></p>
259 </pre><div class="caption">slots and attributes template - <a href="listings/slots-attributes-1.xml"><span class="filename">listings/slots-attributes-1.xml</span></a></div></div>
261 <div class="py-listing"><pre><p class="py-linenumber"> 1
273 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">XMLFile</span>
274 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
276 <span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleElement</span>(<span class="py-src-parameter">Element</span>):
277 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'slots-attributes-1.xml'</span>))
279 @<span class="py-src-variable">renderer</span>
280 <span class="py-src-keyword">def</span> <span class="py-src-identifier">person_profile</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
281 <span class="py-src-comment"># Note how convenient it is to pass these attributes in!</span>
282 <span class="py-src-variable">tag</span>.<span class="py-src-variable">fillSlots</span>(<span class="py-src-variable">person_name</span>=<span class="py-src-string">'Luke'</span>,
283 <span class="py-src-variable">profile_image_url</span>=<span class="py-src-string">'http://example.com/user.png'</span>)
284 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>
285 </pre><div class="caption">slots and attributes element - <a href="listings/slots_attributes_1.py"><span class="filename">listings/slots_attributes_1.py</span></a></div></div>
286 <div class="html-listing"><pre class="htmlsource">
287 <div class="profile">
288 <img src="http://example.com/user.png" />
289 <p>Luke</p>
291 </pre><div class="caption">slots and attributes output - <a href="listings/slots-attributes-output.html"><span class="filename">listings/slots-attributes-output.html</span></a></div></div>
293 <h3>Iteration<a name="auto4"/></h3>
295 <p>Often, you will have a sequence of things, and want to render each of them,
296 repeating a part of the template for each one. This can be done by
297 cloning <code>tag</code> in your renderer:</p>
299 <div class="html-listing"><pre class="htmlsource">
300 <ul xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
301 <li t:render="widgets"><t:slot name="widgetName"/></li>
303 </pre><div class="caption">iteration template - <a href="listings/iteration-1.xml"><span class="filename">listings/iteration-1.xml</span></a></div></div>
304 <div class="py-listing"><pre><p class="py-linenumber"> 1
321 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">XMLFile</span>, <span class="py-src-variable">flattenString</span>
322 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
324 <span class="py-src-keyword">class</span> <span class="py-src-identifier">WidgetsElement</span>(<span class="py-src-parameter">Element</span>):
325 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'iteration-1.xml'</span>))
327 <span class="py-src-variable">widgetData</span> = [<span class="py-src-string">'gadget'</span>, <span class="py-src-string">'contraption'</span>, <span class="py-src-string">'gizmo'</span>, <span class="py-src-string">'doohickey'</span>]
329 @<span class="py-src-variable">renderer</span>
330 <span class="py-src-keyword">def</span> <span class="py-src-identifier">widgets</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
331 <span class="py-src-keyword">for</span> <span class="py-src-variable">widget</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">widgetData</span>:
332 <span class="py-src-keyword">yield</span> <span class="py-src-variable">tag</span>.<span class="py-src-variable">clone</span>().<span class="py-src-variable">fillSlots</span>(<span class="py-src-variable">widgetName</span>=<span class="py-src-variable">widget</span>)
334 <span class="py-src-keyword">def</span> <span class="py-src-identifier">printResult</span>(<span class="py-src-parameter">result</span>):
335 <span class="py-src-keyword">print</span> <span class="py-src-variable">result</span>
337 <span class="py-src-variable">flattenString</span>(<span class="py-src-variable">None</span>, <span class="py-src-variable">WidgetsElement</span>()).<span class="py-src-variable">addCallback</span>(<span class="py-src-variable">printResult</span>)
338 </pre><div class="caption">iteration element - <a href="listings/iteration-1.py"><span class="filename">listings/iteration-1.py</span></a></div></div>
339 <div class="html-listing"><pre class="htmlsource">
341 <li>gadget</li><li>contraption</li><li>gizmo</li><li>doohickey</li>
343 </pre><div class="caption">iteration output - <a href="listings/iteration-output-1.xml"><span class="filename">listings/iteration-output-1.xml</span></a></div></div>
345 <p>This renderer works because a renderer can return anything that can be
346 rendered, not just <code>tag</code>. In this case, we define a generator, which
347 returns a thing that is iterable. We also could have returned
348 a <code>list</code>. Anything that is iterable will be rendered by <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.html" title="twisted.web.template">twisted.web.template</a></code> rendering each item in it. In
349 this case, each item is a copy of the tag the renderer received, each filled
350 with the name of a widget.</p>
352 <h3>Sub-views<a name="auto5"/></h3>
354 <p>Another common pattern is to delegate the rendering logic for a small part of
355 the page to a separate <code>Element</code>. For example, the widgets from the
356 iteration example above might be more complicated to render. You can define
357 an <code>Element</code> subclass which can render a single widget. The renderer
358 method on the container can then yield instances of this
359 new <code>Element</code> subclass.</p>
361 <div class="py-listing"><pre><p class="py-linenumber">1
364 </p><<span class="py-src-variable">ul</span> <span class="py-src-variable">xmlns</span>:<span class="py-src-variable">t</span>=<span class="py-src-string">"http://twistedmatrix.com/ns/twisted.web.template/0.1"</span>>
365 <<span class="py-src-variable">li</span> <span class="py-src-variable">t</span>:<span class="py-src-variable">render</span>=<span class="py-src-string">"widgets"</span>><<span class="py-src-variable">span</span> <span class="py-src-variable">t</span>:<span class="py-src-variable">render</span>=<span class="py-src-string">"name"</span> /></<span class="py-src-variable">li</span>>
366 </<span class="py-src-variable">ul</span>>
367 </pre><div class="caption">subview template - <a href="listings/subviews-1.xml"><span class="filename">listings/subviews-1.xml</span></a></div></div>
368 <div class="py-listing"><pre><p class="py-linenumber"> 1
395 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> (
396 <span class="py-src-variable">XMLFile</span>, <span class="py-src-variable">TagLoader</span>, <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">flattenString</span>)
397 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
399 <span class="py-src-keyword">class</span> <span class="py-src-identifier">WidgetsElement</span>(<span class="py-src-parameter">Element</span>):
400 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'subviews-1.xml'</span>))
402 <span class="py-src-variable">widgetData</span> = [<span class="py-src-string">'gadget'</span>, <span class="py-src-string">'contraption'</span>, <span class="py-src-string">'gizmo'</span>, <span class="py-src-string">'doohickey'</span>]
404 @<span class="py-src-variable">renderer</span>
405 <span class="py-src-keyword">def</span> <span class="py-src-identifier">widgets</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
406 <span class="py-src-keyword">for</span> <span class="py-src-variable">widget</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">widgetData</span>:
407 <span class="py-src-keyword">yield</span> <span class="py-src-variable">WidgetElement</span>(<span class="py-src-variable">TagLoader</span>(<span class="py-src-variable">tag</span>), <span class="py-src-variable">widget</span>)
409 <span class="py-src-keyword">class</span> <span class="py-src-identifier">WidgetElement</span>(<span class="py-src-parameter">Element</span>):
410 <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">loader</span>, <span class="py-src-parameter">name</span>):
411 <span class="py-src-variable">Element</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>, <span class="py-src-variable">loader</span>)
412 <span class="py-src-variable">self</span>.<span class="py-src-variable">_name</span> = <span class="py-src-variable">name</span>
414 @<span class="py-src-variable">renderer</span>
415 <span class="py-src-keyword">def</span> <span class="py-src-identifier">name</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
416 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">_name</span>)
418 <span class="py-src-keyword">def</span> <span class="py-src-identifier">printResult</span>(<span class="py-src-parameter">result</span>):
419 <span class="py-src-keyword">print</span> <span class="py-src-variable">result</span>
421 <span class="py-src-variable">flattenString</span>(<span class="py-src-variable">None</span>, <span class="py-src-variable">WidgetsElement</span>()).<span class="py-src-variable">addCallback</span>(<span class="py-src-variable">printResult</span>)
422 </pre><div class="caption">subview element - <a href="listings/subviews-1.py"><span class="filename">listings/subviews-1.py</span></a></div></div>
423 <div class="html-listing"><pre class="htmlsource">
425 <li><span>gadget</span></li><li><span>contraption</span></li><li><span>gizmo</span></li><li><span>doohickey</span></li>
427 </pre><div class="caption">subview output - <a href="listings/subviews-output-1.xml"><span class="filename">listings/subviews-output-1.xml</span></a></div></div>
429 <p><code>TagLoader</code> lets the portion of the overall template related to
430 widgets be re-used for <code>WidgetElement</code>, which is otherwise a
431 normal <code>Element</code> subclass not much different
432 from <code>WidgetsElement</code>. Notice that the <em>name</em> renderer on
433 the <code>span</code> tag in this template is satisfied
434 from <code>WidgetElement</code>, not <code>WidgetsElement</code>.</p>
436 <h3>Transparent<a name="auto6"/></h3>
438 Note how renderers, slots and attributes require you to specify a renderer on
439 some outer HTML element. What if you don't want to be forced to add an element
440 to your DOM just to drop some content into it? Maybe it messes with your
441 layout, and you can't get it to work in IE with that extra <code>div</code>
442 tag? Perhaps you need <code>t:transparent</code>, which allows you to drop some
443 content in without any surrounding "container" tag. For example:
445 <div class="html-listing"><pre class="htmlsource">
446 <div xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
447 <!-- layout decision - these things need to be *siblings* -->
448 <t:transparent t:render="renderer1" />
449 <t:transparent t:render="renderer2" />
452 </pre><div class="caption">transparent template - <a href="listings/transparent-1.xml"><span class="filename">listings/transparent-1.xml</span></a></div></div>
454 <div class="py-listing"><pre><p class="py-linenumber"> 1
467 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">XMLFile</span>
468 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
470 <span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleElement</span>(<span class="py-src-parameter">Element</span>):
471 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'transparent-1.xml'</span>))
473 @<span class="py-src-variable">renderer</span>
474 <span class="py-src-keyword">def</span> <span class="py-src-identifier">renderer1</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
475 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-string">"hello"</span>)
477 @<span class="py-src-variable">renderer</span>
478 <span class="py-src-keyword">def</span> <span class="py-src-identifier">renderer2</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
479 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-string">"world"</span>)
480 </pre><div class="caption">transparent element - <a href="listings/transparent_element.py"><span class="filename">listings/transparent_element.py</span></a></div></div>
482 <div class="html-listing"><pre class="htmlsource">
484 <!-- layout decision - these things need to be *siblings* -->
488 </pre><div class="caption">transparent rendering output - <a href="listings/transparent-output.html"><span class="filename">listings/transparent-output.html</span></a></div></div>
490 <h2>Quoting<a name="auto7"/></h2>
492 <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.html" title="twisted.web.template">twisted.web.template</a></code> will quote any strings that place
493 into the DOM. This provides protection against <a href="http://en.wikipedia.org/wiki/Cross-site_scripting" shape="rect">XSS attacks</a>, in
494 addition to just generally making it easy to put arbitrary strings onto a web
495 page, without worrying about what they might have in them. This can easily be
496 demonstrated with an element using the same template from our earlier examples.
497 Here's an element that returns some "special" characters in HTML ('<', '>',
498 and '"', which is special in attribute values):
500 <div class="py-listing"><pre><p class="py-linenumber"> 1
513 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">XMLFile</span>
514 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">filepath</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">FilePath</span>
516 <span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleElement</span>(<span class="py-src-parameter">Element</span>):
517 <span class="py-src-variable">loader</span> = <span class="py-src-variable">XMLFile</span>(<span class="py-src-variable">FilePath</span>(<span class="py-src-string">'template-1.xml'</span>))
519 @<span class="py-src-variable">renderer</span>
520 <span class="py-src-keyword">def</span> <span class="py-src-identifier">header</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
521 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-string">'<<<Header>>>!'</span>)
523 @<span class="py-src-variable">renderer</span>
524 <span class="py-src-keyword">def</span> <span class="py-src-identifier">footer</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
525 <span class="py-src-keyword">return</span> <span class="py-src-variable">tag</span>(<span class="py-src-string">'>>>"Footer!"<<<'</span>, <span class="py-src-variable">id</span>=<span class="py-src-string">'<"fun">'</span>)
526 </pre><div class="caption">renderers returning "special" characters - <a href="listings/quoting_element.py"><span class="filename">listings/quoting_element.py</span></a></div></div>
528 Note that they are all safely quoted in the output, and will appear in a web
529 browser just as you returned them from your Python method:
531 <div class="html-listing"><pre class="htmlsource">
534 <div>&lt;&lt;&lt;Header&gt;&gt;&gt;!</div>
535 <div id="content">
536 <p>Content goes here.</p>
538 <div id="&lt;&quot;fun&quot;&gt;">&gt;&gt;&gt;"Footer!"&lt;&lt;&lt;</div>
541 </pre><div class="caption">output containing "special" characters - <a href="listings/quoting-output.html"><span class="filename">listings/quoting-output.html</span></a></div></div>
543 <h2>Deferreds<a name="auto8"/></h2>
545 Finally, a simple demonstration of Deferred support, the unique feature of <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.html" title="twisted.web.template">twisted.web.template</a></code>. Simply put, any renderer may
546 return a Deferred which fires with some template content instead of the template
547 content itself. As shown above, <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.flattenString.html" title="twisted.web.template.flattenString">flattenString</a></code> will return a Deferred that
548 fires with the full content of the string. But if there's a lot of content, you
549 might not want to wait before starting to send some of it to your HTTP client:
550 for that case, you can use <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.flatten.html" title="twisted.web.template.flatten">flatten</a></code>.
551 It's difficult to demonstrate this directly in a browser-based application;
552 unless you insert very long delays before firing your Deferreds, it just looks
553 like your browser is instantly displaying everything. Here's an example that
554 just prints out some HTML template, with markers inserted for where certain
557 <div class="py-listing"><pre><p class="py-linenumber"> 1
589 </p><span class="py-src-keyword">import</span> <span class="py-src-variable">sys</span>
590 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">template</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">XMLString</span>, <span class="py-src-variable">Element</span>, <span class="py-src-variable">renderer</span>, <span class="py-src-variable">flatten</span>
591 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span>.<span class="py-src-variable">defer</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Deferred</span>
593 <span class="py-src-variable">sample</span> = <span class="py-src-variable">XMLString</span>(
594 <span class="py-src-string">"""
595 <div xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
597 <span t:render="wait"></span>
600 """</span>)
602 <span class="py-src-keyword">class</span> <span class="py-src-identifier">WaitForIt</span>(<span class="py-src-parameter">Element</span>):
603 <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>):
604 <span class="py-src-variable">Element</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>, <span class="py-src-variable">loader</span>=<span class="py-src-variable">sample</span>)
605 <span class="py-src-variable">self</span>.<span class="py-src-variable">deferred</span> = <span class="py-src-variable">Deferred</span>()
607 @<span class="py-src-variable">renderer</span>
608 <span class="py-src-keyword">def</span> <span class="py-src-identifier">wait</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>, <span class="py-src-parameter">tag</span>):
609 <span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">deferred</span>.<span class="py-src-variable">addCallback</span>(
610 <span class="py-src-keyword">lambda</span> <span class="py-src-variable">aValue</span>: <span class="py-src-variable">tag</span>(<span class="py-src-string">"A value: "</span> + <span class="py-src-variable">repr</span>(<span class="py-src-variable">aValue</span>)))
612 <span class="py-src-keyword">def</span> <span class="py-src-identifier">done</span>(<span class="py-src-parameter">ignore</span>):
613 <span class="py-src-keyword">print</span>(<span class="py-src-string">"[[[Deferred fired.]]]"</span>)
615 <span class="py-src-keyword">print</span>(<span class="py-src-string">'[[[Rendering the template.]]]'</span>)
616 <span class="py-src-variable">it</span> = <span class="py-src-variable">WaitForIt</span>()
617 <span class="py-src-variable">flatten</span>(<span class="py-src-variable">None</span>, <span class="py-src-variable">it</span>, <span class="py-src-variable">sys</span>.<span class="py-src-variable">stdout</span>.<span class="py-src-variable">write</span>).<span class="py-src-variable">addCallback</span>(<span class="py-src-variable">done</span>)
618 <span class="py-src-keyword">print</span>(<span class="py-src-string">'[[[In progress... now firing the Deferred.]]]'</span>)
619 <span class="py-src-variable">it</span>.<span class="py-src-variable">deferred</span>.<span class="py-src-variable">callback</span>(<span class="py-src-string">"<value>"</span>)
620 <span class="py-src-keyword">print</span>(<span class="py-src-string">'[[[All done.]]]'</span>)
621 </pre><div class="caption">deferred example - <a href="listings/wait_for_it.py"><span class="filename">listings/wait_for_it.py</span></a></div></div>
623 If you run this example, you should get the following output:
625 <div class="html-listing"><pre class="htmlsource">
626 [[[Rendering the template.]]]
629 [[[In progress... now firing the Deferred.]]]
630 <span>A value: '&lt;value&gt;'</span>
632 </div>[[[Deferred fired.]]]
634 </pre><div class="caption">output from deferred example - <a href="listings/waited-for-it.html"><span class="filename">listings/waited-for-it.html</span></a></div></div>
636 This demonstrates that part of the output (everything up to
637 "<code>[[[In progress...</code>") is written out immediately as it's rendered.
638 But once it hits the Deferred, <code>WaitForIt</code>'s rendering needs to pause
639 until <code>.callback(...)</code> is called on that Deferred. You can see that
640 no further output is produced until the message indicating that the Deferred is
641 being fired is complete. By returning Deferreds and using <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.template.flatten.html" title="twisted.web.template.flatten">flatten</a></code>, you can avoid buffering large
644 <h2>A Brief Note on Formats and DOCTYPEs<a name="auto9"/></h2>
647 The goal of <code>twisted.web.template</code> is to emit both valid <a href="http://whatwg.org/html" shape="rect">HTML</a> or <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-xhtml-syntax.html#the-xhtml-syntax" shape="rect">XHTML</a>.
648 However, in order to get the maximally standards-compliant output format you
649 desire, you have to know which one you want, and take a few simple steps to emit
650 it correctly. Many browsers will probably work with most output if you ignore
651 this section entirely, but <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#the-doctype" shape="rect">the
652 HTML specification recommends that you specify an appropriate DOCTYPE</a>.
656 As a <code>DOCTYPE</code> declaration in your template would describe the
657 template itself, rather than its output, it won't be included in your output.
658 If you wish to annotate your template output with a DOCTYPE, you will have to
659 write it to the browser out of band. One way to do this would be to simply
660 do <code>request.write('<!DOCTYPE html>\n')</code> when you are ready to
661 begin emitting your response. The same goes for an XML <code>DOCTYPE</code>
666 <code>twisted.web.template</code> will remove the <code>xmlns</code> attributes
668 the <code>http://twistedmatrix.com/ns/twisted.web.template/0.1</code> namespace,
669 but it will not modify other namespace declaration attributes. Therefore if you
670 wish to serialize in HTML format, you should not use other namespaces; if you
671 wish to serialize to XML, feel free to insert any namespace declarations that
672 are appropriate, and they will appear in your output.
675 <div class="note"><strong>Note: </strong>
676 This relaxed approach is correct in many cases. However, in certain contexts -
677 especially <script> and <style> tags - quoting rules differ in
678 significant ways between HTML and XML, and between different browsers' parsers
679 in HTML. If you want to generate dynamic content inside a script or stylesheet,
680 the best option is to load the resource externally so you don't have to worry
681 about quoting rules. The second best option is to strictly configure your
682 content-types and DOCTYPE declarations for XML, whose quoting rules are simple
683 and compatible with the approach that <code>twisted.web.template</code> takes.
684 And, please remember: regardless of how you put it there, any user input placed
685 inside a <script> or <style> tag is a potential security issue.
688 <h2>A Bit of History<a name="auto10"/></h2>
690 Those of you who used Divmod Nevow may notice some
691 similarities. <code>twisted.web.template</code> is in fact derived from the
692 latest version of Nevow, but includes only the latest components from Nevow's
693 rendering pipeline, and does not have any of the legacy compatibility layers
694 that Nevow grew over time. This should make
695 using <code>twisted.web.template</code> a similar experience for many long-time
696 users of Twisted who have previously used Nevow for its twisted-friendly
697 templating, but more straightforward for new users.
701 <p><a href="index.html">Index</a></p>
702 <span class="version">Version: 12.1.0</span>