~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/web/howto/resource-templates.html

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 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>
 
21
 
 
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>
 
27
 
 
28
<h2>Configuring Twisted.Web<a name="auto1"/></h2>
 
29
 
 
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>
 
33
 
 
34
<pre xml:space="preserve">
 
35
% twistd web --path=/var/www \
 
36
        --processor=.rtl=twisted.web.script.ResourceTemplate
 
37
</pre>
 
38
 
 
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>
 
42
 
 
43
<h2>Using ResourceTemplate<a name="auto2"/></h2>
 
44
 
 
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>
 
54
 
 
55
<p>Here is a simple example for a resource template.</p>
 
56
 
 
57
<div class="py-listing"><pre><p class="py-linenumber"> 1
 
58
 2
 
59
 3
 
60
 4
 
61
 5
 
62
 6
 
63
 7
 
64
 8
 
65
 9
 
66
10
 
67
11
 
68
12
 
69
13
 
70
14
 
71
15
 
72
16
 
73
17
 
74
18
 
75
19
 
76
20
 
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>
 
78
 
 
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">&quot;An apple a day keeps the doctor away.&quot;</span>
 
81
 
 
82
 
 
83
<span class="py-src-keyword">class</span> <span class="py-src-identifier">QuoteResource</span>(<span class="py-src-parameter">Resource</span>):
 
84
 
 
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">&quot;&quot;&quot;\
 
87
        &lt;html&gt;
 
88
        &lt;head&gt;&lt;title&gt;Quotes Galore&lt;/title&gt;&lt;/head&gt;
 
89
 
 
90
        &lt;body&gt;&lt;h1&gt;Quotes&lt;/h1&gt;&quot;&quot;&quot;</span>
 
91
        <span class="py-src-variable">getQuote</span>()
 
92
        <span class="py-src-string">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span>
 
93
 
 
94
 
 
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>
 
97
 
 
98
</div>
 
99
 
 
100
    <p><a href="index.html">Index</a></p>
 
101
    <span class="version">Version: 10.0.0</span>
 
102
  </body>
 
103
</html>
 
 
b'\\ No newline at end of file'