[Twisted-Python] woven headache

Donovan Preston dp at twistedmatrix.com
Mon Dec 16 02:32:59 EST 2002


A List of Links into HTML

The easiest way to do this is to use the List and Anchor widgets to 
handle the list and a string, respectively. If you had an RPY that 
looked like this (the main model is a list of strings):

LinkList.rpy
----
from twisted.web.woven import page

resource = page.Page(['http://www.google.com', 'http://www.foo.com'], 
templateFile="LinkList.html")
----

Your template file would look like this:

LinkList.html
----
<html>
	<ul view="List" model=".">
		<!-- Use the current model as this node's model.
			Use the List widget in widgets.py to turn this model into html. -->

		<li pattern="listItem">
			<!-- The pattern node will be copied once for each element in the 
list
				by the List widget. Each copy will have a model= attribute with
				the index of the current list item set. When woven recurses into
				the new, copied nodes, the list element will be pushed onto the
				stack. -->
			<a view="Anchor" model="." />
			<!-- When woven descended into the li node, it pushed a string onto
				the model stack. We now want to render that string into HTML
				using the Anchor widget. We specify ".", the top of the model stack.
			-->
		</li>
	</ul>
</html>
----

There you go.

Unfortunately the base Anchor widget is pretty dumb. It just takes a 
string model, and sets the node href= to the string, and adds a text 
node to the <a> node. Next installment: writing a custom widget that 
does fancier stuff.

Donovan





More information about the Twisted-Python mailing list