resource-templates.xhtml   [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">
  <head>
    <title>Light Weight Templating With Resource Templates</title>
  </head>

  <body>
    <h1>Light Weight Templating With Resource Templates</h1>

<h2>Overview</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.xhtml">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</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</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">twisted.web.server.Resource</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>

<a href="listings/TwistedQuotes/webquote.rtl" class="py-listing">Resource Template for Quotes</a>

</body>
</html>