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
16 example, <a href="https://launchpad.net/nevow" shape="rect">Divmod
17 Nevow</a>, sometimes one needs a less file-heavy system which lets one
18 directly write HTML. While
19 <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.script.ResourceScript.html" title="twisted.web.script.ResourceScript">ResourceScript</a></code> is
20 available, it has a high coding overhead, and requires some boring string
22 <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.script.ResourceTemplate.html" title="twisted.web.script.ResourceTemplate">ResourceTemplate</a></code> fills the
23 space between Nevow and ResourceScript using Quixote's PTL (Python Templating
26 <p>ResourceTemplates need Quixote
27 installed. In <a href="http://www.debian.org" shape="rect">Debian</a>, that means
28 installing the <code>python-quixote</code> package
29 (<code>apt-get install python-quixote</code>). Other operating systems
30 require other ways to install Quixote, or it can be done manually.</p>
32 <h2>Configuring Twisted Web<a name="auto1"/></h2>
34 <p>The easiest way to get Twisted Web to support ResourceTemplates is to
35 bind them to some extension using the web tap's <code>--processor</code>
36 flag. Here is an example:</p>
38 <pre xml:space="preserve">
39 % twistd web --path=/var/www \
40 --processor=.rtl=twisted.web.script.ResourceTemplate
43 <p>The above command line binds the <code>rtl</code> extension to use the
44 ResourceTemplate processor. Other ways are possible, but would require
45 more Python coding and are outside the scope of this HOWTO.</p>
47 <h2>Using ResourceTemplate<a name="auto2"/></h2>
49 <p>ResourceTemplates are coded in an extension of Python called the
50 <q>Python Templating Language</q>. Complete documentation of the PTL
52 at <a href="http://quixote.python.ca/quixote.dev/doc/PTL.html" shape="rect">the
53 quixote web site</a>. The web server will expect the PTL source file
54 to define a variable named <code>resource</code>. This should be
55 a <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">twisted.web.resource.Resource</a></code>,
56 whose <code>.render</code> method be called. Usually, you would want
57 to define <code>render</code> using the keyword <code>template</code>
58 rather than <code>def</code>.</p>
60 <p>Here is a simple example for a resource template.</p>
62 <div class="py-listing"><pre><p class="py-linenumber"> 1
82 </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>
84 <span class="py-src-keyword">def</span> <span class="py-src-identifier">getQuote</span>():
85 <span class="py-src-keyword">return</span> <span class="py-src-string">"An apple a day keeps the doctor away."</span>
88 <span class="py-src-keyword">class</span> <span class="py-src-identifier">QuoteResource</span>(<span class="py-src-parameter">Resource</span>):
90 <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>):
91 <span class="py-src-string">"""\
93 <head><title>Quotes Galore</title></head>
95 <body><h1>Quotes</h1>"""</span>
96 <span class="py-src-variable">getQuote</span>()
97 <span class="py-src-string">"</body></html>"</span>
100 <span class="py-src-variable">resource</span> = <span class="py-src-variable">QuoteResource</span>()
101 </pre><div class="caption">Resource Template for Quotes - <a href="listings/webquote.rtl"><span class="filename">listings/webquote.rtl</span></a></div></div>
105 <p><a href="index.html">Index</a></p>
106 <span class="version">Version: 12.1.0</span>