resource-templates.html   [plain text]


<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><title>Twisted Documentation: Light Weight Templating With Resource Templates</title><link href="../howto/stylesheet.css" type="text/css" rel="stylesheet" /></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></span><h2>Overview<a name="auto0"></a></h2><p>While Twisted supports solution like the <a href="http://www.divmod.org/Home/Projects/Nevow/">Nevow</a> framework (available as part of the <a href="http://www.divmod.org/Quotient/">Quotient</a> project)
for high-content sophisticated templating needs, 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
<a href="woven.html">Woven</a> and ResourceScript using Quixote's PTL (Python Templating Language).</p><p>ResourceTemplates need Quixote installed. In 
<a href="http://www.debian.org">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"></a></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>
% mktap 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"></a></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">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/TwistedDocs/TwistedDocs-1.3.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>
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span><span class="py-src-op">.</span><span class="py-src-variable">web</span><span class="py-src-op">.</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-newline">
</span><span class="py-src-keyword">from</span> <span class="py-src-variable">TwistedQuotes</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">quoters</span><span class="py-src-newline">
</span><span class="py-src-nl">
</span><span class="py-src-variable">quotefile</span> <span class="py-src-op">=</span> <span class="py-src-variable">os</span><span class="py-src-op">.</span><span class="py-src-variable">path</span><span class="py-src-op">.</span><span class="py-src-variable">join</span><span class="py-src-op">(</span><span class="py-src-variable">os</span><span class="py-src-op">.</span><span class="py-src-variable">path</span><span class="py-src-op">.</span><span class="py-src-variable">split</span><span class="py-src-op">(</span><span class="py-src-variable">__file__</span><span class="py-src-op">)</span><span class="py-src-op">[</span><span class="py-src-number">0</span><span class="py-src-op">]</span><span class="py-src-op">,</span> <span class="py-src-string">&quot;quotes.txt&quot;</span><span class="py-src-op">)</span><span class="py-src-newline">
</span><span class="py-src-nl">
</span><span class="py-src-variable">quoter</span> <span class="py-src-op">=</span> <span class="py-src-variable">quoters</span><span class="py-src-op">.</span><span class="py-src-variable">FortuneQuoter</span><span class="py-src-op">(</span><span class="py-src-op">[</span><span class="py-src-variable">quotefile</span><span class="py-src-op">]</span><span class="py-src-op">)</span><span class="py-src-newline">
</span><span class="py-src-nl">
</span><span class="py-src-nl">
</span><span class="py-src-keyword">class</span> <span class="py-src-identifier">QuoteResource</span><span class="py-src-op">(</span><span class="py-src-parameter">Resource</span><span class="py-src-op">)</span><span class="py-src-op">:</span><span class="py-src-newline">
</span><span class="py-src-nl">
</span><span class="py-src-indent">    </span><span class="py-src-variable">template</span> <span class="py-src-variable">render</span><span class="py-src-op">(</span><span class="py-src-variable">self</span><span class="py-src-op">,</span> <span class="py-src-variable">request</span><span class="py-src-op">)</span><span class="py-src-op">:</span><span class="py-src-newline">
</span><span class="py-src-indent">        </span><span class="py-src-string">&quot;&quot;&quot;\
        &lt;html&gt;
        &lt;head&gt;&lt;title&gt;Quotes Galore&lt;/title&gt;&lt;/head&gt;

        &lt;body&gt;&lt;h1&gt;Quotes&lt;/h1&gt;&quot;&quot;&quot;</span>                                <span class="py-src-newline">
</span>        <span class="py-src-variable">quoter</span><span class="py-src-op">.</span><span class="py-src-variable">getQuote</span><span class="py-src-op">(</span><span class="py-src-op">)</span><span class="py-src-newline">
</span>        <span class="py-src-string">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span><span class="py-src-newline">
</span><span class="py-src-nl">
</span><span class="py-src-nl">
</span><span class="py-src-dedent"></span><span class="py-src-dedent"></span><span class="py-src-variable">resource</span> <span class="py-src-op">=</span> <span class="py-src-variable">QuoteResource</span><span class="py-src-op">(</span><span class="py-src-op">)</span><span class="py-src-newline">
</span><span class="py-src-endmarker"></span></pre><div class="caption">Resource Template for Quotes - <a href="listings/TwistedQuotes/webquote.rtl"><span class="filename">listings/TwistedQuotes/webquote.rtl</span></a></div></div></div><p><a href="../howto/index.html">Index</a></p><span class="version">Version: 1.3.0</span></body></html>