t.w.w.template : module documentation

Part of twisted.web.woven View Source

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.

A short example:
  | class Test(DOMTemplate):
  |     template = '''
  | <html><head><title>Foo</title></head><body>
  |
  | <div view="Test">
  | This test node will be replaced
  | </div>
  |
  | </body></html>
  | '''
  |
  |     def factory_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
Interface INodeMutator A component that implements NodeMutator knows how to mutate DOM based on the instructions in the object it wraps.
Class NodeMutator Undocumented
Class NodeNodeMutator A NodeNodeMutator replaces the node that is passed in to generate with the node it adapts.
Class NoneNodeMutator Undocumented
Class StringNodeMutator A StringNodeMutator replaces the node that is passed in to generate with the string it adapts.
Class DOMTemplate A resource that renders pages using DOM.
Class DOMController A simple controller that automatically passes responsibility on to the view class registered for the model. You can override render to perform more advanced template lookup logic.
API Documentation for Twisted, generated by pydoctor at 2011-10-27 15:57:47.