[Twisted-Python] Surprises in twisted.web.woven

Tim Allen screwtape at froup.com
Sun Aug 3 09:05:48 EDT 2003

I had a little play with Woven tonight, and I'm pleased to report that 
I'm (slowly) getting my head around it quite satisfyingly. However, I 
did come across some things which surprised me - I'm not going to call 
them bugs, because I don't understand Woven well enough.

Firstly, I'm rendering HTML, so my HTML template file starts off with 
an HTML doctype. Woven appears to eat this doctype, which is rather a 
shame - including the proper doctype makes Internet Explorer 6 behave 
much more like Mozilla, which in turn makes writing webpages much less 
stressful. I do wind up with the standard XML header at the top of the 
output, but as I'm not trying to output XML, I'm afraid this might 
confuse something.

Secondly, I was surprised by the behaviour of view templates. See the 
sample rpy code below:

	from twisted.web.woven import page, view

	class TestContent(view.View):
	    template = """<ul model="sampleList" view="List">
	            		<li pattern="listItem" model="." view="Text"/>
			            <li pattern="emptyList"><i>empty</i></li>

	class TestPage(page.Page):
     	template = """<html><body>
                 		<h1 model="title" view="Text"/>
						<div view="content"/>

	    def wvfactory_content(self, request, node, model): return 

	    def wmfactory_title(self, request): return "Test page"

	    def wmfactory_sampleList(self, request): return ["Hello", "world!"]

	resource = TestPage()

What I expected was a list that looked like:

	* Hello
	* world!

What I got was a list that looked like:

	* <__builtin__.TestPage instance at 0x554f08>
     * empty

If I wrap the <ul> element in the template in a <div> (or anything 
else), then the output is correct - but I get the dummy <div> in the 

...it seems that view.View ignores model= and view= attributes on 
top-level elements in templates, and I have no idea why.

Thirdly, looking again at the above sample code - the TestPage template 
contains a reference to a model named 'title', and Woven looks for the 
model 'title' in the TestPage instance. Fair enough. The TestContent 
template contains a reference to a model named 'sampleList', and Woven 
looks for the model 'sampleList'... not in the TestContent instance as 
I would expect, but in the TestPage instance again.

Finally, if I have a template that looks like this:

	<p>Have a look at
	<a href="cat1.png">these</a>
	<a href="cat2.png">three</a>
	<a href="cat3.png">cats</a>.</p>

then the output HTML looks like:

	<p>Have a look at <a href="cat1.png">these</a><a
	href="cat2.png">three</a><a href="cat3.png">cats</a>.</p>

...spaces aren't preserved. I expect an XML processor is allowed to 
collapse whitespace, but not remove it entirely.

More information about the Twisted-Python mailing list