Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / web / howto / resource-templates.html
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">
2   <head>
3 <title>Twisted Documentation: Light Weight Templating With Resource Templates</title>
4 <link href="stylesheet.css" rel="stylesheet" type="text/css"/>
5   </head>
6
7   <body bgcolor="white">
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>
10     <div class="content">
11     <span/>
12
13 <h2>Overview<a name="auto0"/></h2>
14
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
21 arithmetic. 
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
24 Language).</p>
25
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>
31
32 <h2>Configuring Twisted Web<a name="auto1"/></h2>
33
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>
37
38 <pre xml:space="preserve">
39 % twistd web --path=/var/www \
40         --processor=.rtl=twisted.web.script.ResourceTemplate
41 </pre>
42
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>
46
47 <h2>Using ResourceTemplate<a name="auto2"/></h2>
48
49 <p>ResourceTemplates are coded in an extension of Python called the
50 <q>Python Templating Language</q>. Complete documentation of the PTL
51 is available
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>
59
60 <p>Here is a simple example for a resource template.</p>
61
62 <div class="py-listing"><pre><p class="py-linenumber"> 1
63  2
64  3
65  4
66  5
67  6
68  7
69  8
70  9
71 10
72 11
73 12
74 13
75 14
76 15
77 16
78 17
79 18
80 19
81 20
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>
83
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">&quot;An apple a day keeps the doctor away.&quot;</span>
86
87
88 <span class="py-src-keyword">class</span> <span class="py-src-identifier">QuoteResource</span>(<span class="py-src-parameter">Resource</span>):
89
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">&quot;&quot;&quot;\
92         &lt;html&gt;
93         &lt;head&gt;&lt;title&gt;Quotes Galore&lt;/title&gt;&lt;/head&gt;
94
95         &lt;body&gt;&lt;h1&gt;Quotes&lt;/h1&gt;&quot;&quot;&quot;</span>
96         <span class="py-src-variable">getQuote</span>()
97         <span class="py-src-string">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span>
98
99
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>
102
103 </div>
104
105     <p><a href="index.html">Index</a></p>
106     <span class="version">Version: 12.1.0</span>
107   </body>
108 </html>