Table of Contents

Module: domtemplate Twisted-0.19.0/twisted/web/domtemplate.py

DOMTemplate

Most templating systems provide commands that you embed in the HTML to repeat elements, include fragments from other files, etc. This works fairly well for simple constructs and people tend to get a false sense of simplicity from this. However, in my experience, as soon as the programmer wants to make the logic even slightly more complicated, the templating system must be bent and abused in ways it was never meant to be used.

The theory behind DOMTemplate is that Python code instead of template syntax in the HTML should be used to manipulate the structure of the HTML. DOMTemplate uses the DOM, a w3c standard tree-based representation of an HTML document that provides an API that allows you to traverse nodes in the tree, examine their attributes, move, add, and delete them. It is a fairly low level API, meaning it takes quite a bit of code to get a bit done, but it is standard -- learn the DOM once, you can use it from ActionScript, JavaScript, Java, C++, whatever.

A DOMTemplate subclass must do two things: indicate which template it wants to use, and indicate which elements it is interested in.

------------------------------------------------------

class Test(DOMTemplate): template = ''' <html><head><title>Foo</title></head><body>

<div class="Test"> This test node will be replaced </div>

</body></html> '''

def getTemplateMethods(self): return [{'class': Test, 'method': self.test}]

def test(self, request, node): ''' The test method will be called with the request and the DOM node that the test method was associated with. ''' # self.d has been bound to the main DOM "document" object newNode = self.d.createTextNode("Testing, 1,2,3")

# Replace the test node with our single new text node return newNode

Imported modules   
from cStringIO import StringIO
import domhandlers
import domwidgets
import os
from server import NOT_DONE_YET
import stat
import string
from twisted.internet import reactor
from twisted.python import components, defer, failure, mvc, log
from twisted.python.defer import Deferred
from twisted.python.mvc import View, IView, Controller
from twisted.web import resource, widgets, domwidgets
from twisted.web.resource import Resource
import types
from xml.dom import minidom
Classes   
DOMController

A simple controller that automatically passes responsibility on to the view

DOMTemplate

A resource that renders pages using DOM.

DefaultHandler
DefaultWidget
MethodLookup

Table of Contents

This document was automatically generated on Sun Jul 14 19:53:39 2002 by HappyDoc version 2.0