blob: 8e2bf6a8ee4168796232aba046046dcd1388e1ec (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
<?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">
<head>
<title>Twisted Documentation: Light Weight Templating With Resource Templates</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body bgcolor="white">
<h1 class="title">Light Weight Templating With Resource Templates</h1>
<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>
<div class="content">
<span/>
<h2>Overview<a name="auto0"/></h2>
<p>While high-level templating systems can be used with Twisted (for example,
<a href="http://divmod.org/trac/wiki/DivmodNevow" shape="rect">Divmod Nevow</a>, sometimes
one needs a less file-heavy system which lets one directly write HTML. While
ResourceScripts are available, they have a high overhead of coding, needing
some boring string arithmetic. ResourceTemplates fill the space between Nevow
and ResourceScript using Quixote's PTL (Python Templating Language).</p>
<p>ResourceTemplates need Quixote installed. In
<a href="http://www.debian.org" shape="rect">Debian</a>, that means using Python 2.2
and installing the <code>quixote</code> package
(<code>apt-get install quixote</code>). Other operating systems require
other ways to install quixote, or it can be done manually.</p>
<h2>Configuring Twisted.Web<a name="auto1"/></h2>
<p>The easiest way to get Twisted.Web to support ResourceTemplates is to
bind them to some extension using the web tap's <code>--processor</code>
flag. Here is an example:</p>
<pre xml:space="preserve">
% twistd web --path=/var/www \
--processor=.rtl=twisted.web.script.ResourceTemplate
</pre>
<p>The above command line binds the <code>rtl</code> extension to use the
ResourceTemplate processor. Other ways are possible, but would require
more Python coding and are outside the scope of this HOWTO.</p>
<h2>Using ResourceTemplate<a name="auto2"/></h2>
<p>ResourceTemplates are coded in an extension of Python called the
<q>Python Templating Language</q>. Complete documentation of the PTL
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
will expect the PTL source file to define a variable named
<code>resource</code>.
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>,
whose <code>.render</code> method be called. Usually, you would want
to define <code>render</code> using the keyword <code>template</code>
rather than <code>def</code>.</p>
<p>Here is a simple example for a resource template.</p>
<div class="py-listing"><pre><p class="py-linenumber"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</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>
<span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
<span class="py-src-keyword">return</span> <span class="py-src-string">"An apple a day keeps the doctor away."</span>
<span class="py-src-keyword">class</span> <span class="py-src-identifier">QuoteResource</span>(<span class="py-src-parameter">Resource</span>):
<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>):
<span class="py-src-string">"""\
<html>
<head><title>Quotes Galore</title></head>
<body><h1>Quotes</h1>"""</span>
<span class="py-src-variable">getQuote</span>()
<span class="py-src-string">"</body></html>"</span>
<span class="py-src-variable">resource</span> = <span class="py-src-variable">QuoteResource</span>()
</pre><div class="caption">Resource Template for Quotes - <a href="listings/webquote.rtl"><span class="filename">listings/webquote.rtl</span></a></div></div>
</div>
<p><a href="index.html">Index</a></p>
<span class="version">Version: 10.0.0</span>
</body>
</html>
|