Skip to content

Latest commit

 

History

History
97 lines (44 loc) · 2.26 KB

resource-templates.rst

File metadata and controls

97 lines (44 loc) · 2.26 KB
LastChangedDate

$LastChangedDate$

LastChangedRevision

$LastChangedRevision$

LastChangedBy

$LastChangedBy$

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 twisted.web.script.ResourceScript <ResourceScript> is available, it has a high coding overhead, and requires some boring string arithmetic. twisted.web.script.ResourceTemplate <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 <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 <listings/webquote.rtl>

listings/webquote.rtl