HOME FAQ DOCS DOWNLOAD

### Navigation

- index
- next |
- previous |
- Twisted 14.0.1 documentation »
- Twisted Web »
- Developer Guides »

Go to the latest version of this document.

# Light Weight Templating With Resource Templates

## Overview

While high-level templating systems can be used with Twisted (for example, DivmodNevow , sometimes one needs a less file-heavy system which lets one directly write HTML. While ResourceScript is available, it has a high coding overhead, and requires some boring string arithmetic. ResourceTemplate fills the space between Nevow and ResourceScript using Quixote’s PTL (Python Templating Language).

ResourceTemplates need Quixote installed. In Debian , that means installing the `python-quixote` package (`apt-get install python-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:

    % twistd 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.resource.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.

`webquote.rtl`

    from twisted.web.resource import Resource

    def getQuote():
        return "An apple a day keeps the doctor away."

    class QuoteResource(Resource):

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

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

    resource = QuoteResource()