Package twisted :: Package web :: Package woven :: Module template
[frames | no frames]

Module twisted.web.woven.template

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

Classes
DOMController A simple controller that automatically passes responsibility on to the view class registered for the model.
DOMTemplate A resource that renders pages using DOM.
DOMView A resource that renders pages using DOM.
INodeMutator A component that implements NodeMutator knows how to mutate DOM based on the instructions in the object it wraps.
NodeMutator  
NodeNodeMutator A NodeNodeMutator replaces the node that is passed in to generate with the node it adapts.
NoneNodeMutator  
StringNodeMutator A StringNodeMutator replaces the node that is passed in to generate with the string it adapts.

Variable Summary
int RESTART_RENDERING = 2                                                                     
int STOP_RENDERING = 1                                                                     

Variable Details

RESTART_RENDERING

Type:
int
Value:
2                                                                     

STOP_RENDERING

Type:
int
Value:
1                                                                     

Generated by Epydoc 2.0 on Sat May 15 20:08:32 2004 http://epydoc.sf.net