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: Light Weight Templating With Resource Templates</title>
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">Light Weight Templating With Resource Templates</h1>
9
<div class="toc"><ol><li><a href="#auto0">Overview</a></li><li><a href="#auto1">Configuring Twisted.Web</a></li><li><a href="#auto2">Using ResourceTemplate</a></li></ol></div>
13
<h2>Overview<a name="auto0"/></h2>
15
<p>While high-level templating systems can be used with Twisted (for example,
16
<a href="http://divmod.org/trac/wiki/DivmodNevow" shape="rect">Divmod Nevow</a>, sometimes
17
one needs a less file-heavy system which lets one directly write HTML. While
18
ResourceScripts are available, they have a high overhead of coding, needing
19
some boring string arithmetic. ResourceTemplates fill the space between Nevow
20
and ResourceScript using Quixote's PTL (Python Templating Language).</p>
22
<p>ResourceTemplates need Quixote installed. In
23
<a href="http://www.debian.org" shape="rect">Debian</a>, that means using Python 2.2
24
and installing the <code>quixote</code> package
25
(<code>apt-get install quixote</code>). Other operating systems require
26
other ways to install quixote, or it can be done manually.</p>
28
<h2>Configuring Twisted.Web<a name="auto1"/></h2>
30
<p>The easiest way to get Twisted.Web to support ResourceTemplates is to
31
bind them to some extension using the web tap's <code>--processor</code>
32
flag. Here is an example:</p>
34
<pre xml:space="preserve">
35
% twistd web --path=/var/www \
36
--processor=.rtl=twisted.web.script.ResourceTemplate
39
<p>The above command line binds the <code>rtl</code> extension to use the
40
ResourceTemplate processor. Other ways are possible, but would require
41
more Python coding and are outside the scope of this HOWTO.</p>
43
<h2>Using ResourceTemplate<a name="auto2"/></h2>
45
<p>ResourceTemplates are coded in an extension of Python called the
46
<q>Python Templating Language</q>. Complete documentation of the PTL
47
is available at <a href="http://www.mems-exchange.org/software/quixote/doc/PTL.html" shape="rect">the quixote web site</a>. The web server
48
will expect the PTL source file to define a variable named
49
<code>resource</code>.
50
This should be a <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Resource.html" title="twisted.web.server.Resource">twisted.web.server.Resource</a></code>,
51
whose <code>.render</code> method be called. Usually, you would want
52
to define <code>render</code> using the keyword <code>template</code>
53
rather than <code>def</code>.</p>
55
<p>Here is a simple example for a resource template.</p>
57
<div class="py-listing"><pre><p class="py-linenumber"> 1
77
</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">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
79
<span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
80
<span class="py-src-keyword">return</span> <span class="py-src-string">"An apple a day keeps the doctor away."</span>
83
<span class="py-src-keyword">class</span> <span class="py-src-identifier">QuoteResource</span>(<span class="py-src-parameter">Resource</span>):
85
<span class="py-src-variable">template</span> <span class="py-src-variable">render</span>(<span class="py-src-variable">self</span>, <span class="py-src-variable">request</span>):
86
<span class="py-src-string">"""\
88
<head><title>Quotes Galore</title></head>
90
<body><h1>Quotes</h1>"""</span>
91
<span class="py-src-variable">getQuote</span>()
92
<span class="py-src-string">"</body></html>"</span>
95
<span class="py-src-variable">resource</span> = <span class="py-src-variable">QuoteResource</span>()
96
</pre><div class="caption">Resource Template for Quotes - <a href="listings/webquote.rtl"><span class="filename">listings/webquote.rtl</span></a></div></div>
100
<p><a href="index.html">Index</a></p>
101
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'