1 2:LastChangedDate: $LastChangedDate$ 3:LastChangedRevision: $LastChangedRevision$ 4:LastChangedBy: $LastChangedBy$ 5 6Light Weight Templating With Resource Templates 7=============================================== 8 9 10 11 12 13 14Overview 15-------- 16 17 18 19While high-level templating systems can be used with Twisted (for 20example, `DivmodNevow <https://launchpad.net/nevow>`_ , sometimes one needs a less file-heavy system which lets one 21directly write HTML. While :py:func:`ResourceScript <twisted.web.script.ResourceScript>` is 22available, it has a high coding overhead, and requires some boring string 23arithmetic. :py:func:`ResourceTemplate <twisted.web.script.ResourceTemplate>` fills the 24space between Nevow and ResourceScript using Quixote's PTL (Python Templating 25Language). 26 27 28 29 30ResourceTemplates need Quixote 31installed. In `Debian <http://www.debian.org>`_ , that means 32installing the ``python-quixote`` package 33(``apt-get install python-quixote`` ). Other operating systems 34require other ways to install Quixote, or it can be done manually. 35 36 37 38 39 40Configuring Twisted Web 41----------------------- 42 43 44 45The easiest way to get Twisted Web to support ResourceTemplates is to 46bind them to some extension using the web tap's ``--processor`` 47flag. Here is an example: 48 49 50 51 52 53:: 54 55 56 % twistd web --path=/var/www \ 57 --processor=.rtl=twisted.web.script.ResourceTemplate 58 59 60 61 62The above command line binds the ``rtl`` extension to use the 63ResourceTemplate processor. Other ways are possible, but would require 64more Python coding and are outside the scope of this HOWTO. 65 66 67 68 69 70Using ResourceTemplate 71---------------------- 72 73 74 75ResourceTemplates are coded in an extension of Python called the"Python Templating Language" . Complete documentation of the PTL 76is available 77at `the quixote web site <http://quixote.python.ca/quixote.dev/doc/PTL.html>`_ . The web server will expect the PTL source file 78to define a variable named ``resource`` . This should be 79a :py:class:`twisted.web.resource.Resource` , 80whose ``.render`` method be called. Usually, you would want 81to define ``render`` using the keyword ``template`` 82rather than ``def`` . 83 84 85 86 87Here is a simple example for a resource template. 88 89 90 91 92 93:download:`webquote.rtl <listings/webquote.rtl>` 94 95.. literalinclude:: listings/webquote.rtl 96 :language: py3 97 98