[Twisted-Python] Newbie Q on 0.99 release

Donovan Preston dp at twistedmatrix.com
Fri Aug 23 02:26:29 EDT 2002

On Thursday, August 22, 2002, at 06:57 AM, Prabhakar Chaganti wrote:

> All:
> I have just started playing with twisted and have a few q:
> 1. What is the difference between web widgets and the DOM templates?

I'll spend a bit of time answering this since I wrote them :-)

DOMTemplate is a class that parses an XHTML document looking for nodes 
with "view", "controller", "class", or "id" attributes. When it finds 
one, it looks for a "factory_foo" method to handle that node. (e.g. 
when it encounters the node <div id="coolStuff" />, the method 
factory_coolStuff would be called.) This method gets passed the request 
object and the current node object (as a DOM Node object). The return 
value of this method is then used to replace the node that was there 

Here is an example:

from twisted.web.domtemplate import DOMTemplate
from twisted.web import domwidgets

class Foo(DOMTemplate):
	template = '<html><div id="foo" /> <div id="bar" /> <div id="baz" 

	def factory_foo(self, request, node):
		# I have been passed a DOM Node instance, I can mutate it and return 
		# The main DOM Document instance (where the DOM factories are)
		# has been set to self.d
		node.appendChild(self.d.createTextNode("Hello world!"))
		return node

	def factory_bar(self, request, node):
		# Or, if I wish, I may simply return a string. It will be parsed into 
XML and
		# will replace the incoming node.
		return "<b>Hey, how are you doing?</b>"

	def factory_baz(self, request, node):
		# However, I may wish to use a higher level API that involves 
		# Python objects instead of HTML. By using widgets, I can construct 
		# to represent common HTML fragments, and never have to write any HTML
		# again! (Ok, so you'll have to write some eventually... but less)
		return domwidgets.Text("Hey, this is a very simple widget that knows 
how to turn itself into DOM text nodes using createTextNode")

So you can see that DOMWidgets builds upon DOMTemplate by allowing you 
to use higher-level APIs in your DOMTemplate.


More information about the Twisted-Python mailing list