# Light Weight Templating With Resource Templates

## Overview

While Twisted supports solution like the Nevow framework (available as part of the Quotient 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 Woven and ResourceScript using Quixote's PTL (Python Templating Language).

ResourceTemplates need Quixote installed. In Debian, that means using Python 2.2 and installing the `quixote` package (`apt-get install quixote`). Other operating systems require other ways to install quixote, or it can be done manually.

## Configuring Twisted.Web

The easiest way to get Twisted.Web to support ResourceTemplates is to bind them to some extension using the web tap's `--processor` flag. Here is an example:

    % mktap web --path=/var/www \
            --processor=.rtl=twisted.web.script.ResourceTemplate

The above command line binds the `rtl` extension to use the ResourceTemplate processor. Other ways are possible, but would require more Python coding and are outside the scope of this HOWTO.

## Using ResourceTemplate

ResourceTemplates are coded in an extension of Python called the Python Templating Language. Complete documentation of the PTL is available at the quixote web site. The web server will expect the PTL source file to define a variable named `resource`. This should be a `twisted.web.server.Resource`, whose `.render` method be called. Usually, you would want to define `render` using the keyword `template` rather than `def`.

Here is a simple example for a resource template.

    from twisted.web.resource import Resource
    from TwistedQuotes import quoters

    quotefile = os.path.join(os.path.split(__file__)[0], "quotes.txt")

    quoter = quoters.FortuneQuoter([quotefile])

    class QuoteResource(Resource):

        template render(self, request):
            """\
            <html>
            <head><title>Quotes Galore</title></head>

            <body><h1>Quotes</h1>"""
            quoter.getQuote()
            "</body></html>"

    resource = QuoteResource()

Resource Template for Quotes - listings/TwistedQuotes/webquote.rtl

Index

Version: 1.3.0