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

Alex Levy mesozoic at polynode.com
Sun Aug 3 11:37:27 EDT 2003

On Sun, Aug 03, 2003 at 11:05:48PM +1000, Tim Allen wrote:
> 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.

Don't you know anything about software development? These are FEATURES :)

You've hit a problem that I encountered as well; the root element in a Woven
template doesn't behave the same way as its child elements. What you want is

  class TestContent(view.View):
    template = """
    <div><ul model="sampleList" view="List">...</ul></div>

I believe this is one of those "just accept it" situations. Fortunately,
wrapping your work in <div></div>'s isn't that bad; they don't affect layout
(unless your CSS puts padding on every div, which is nuts), and HTML
pretty-printing has been added to CVS in time for the next release.

On to your models question. Views must be given a model to operate on; they
do not (as far as I know) intrinsically contain models of their own. When
your <div view="content"/> element is given no explicit model in the
template, it is given the entire Page as its model. (That means the 'model'
object being passed to wvfactory_content is actually the Page instance.)
This works because it looks for the sampleList submodel on whatever model
it's been given.

Unless you have a good reason for passing the entire Page model to your
views (and you might), another way to do this is:

  class TestContent(view.View):
    template = """
    <div><ul model="." view="List">...</ul></div>
  class TestPage(page.Page):
    template = """
    <html>...<div model="sampleList" view="content"/>...</html>

> 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>

This is another issue that I've wrangled with for some time. Feel free to
make a lot of noise and hope somebody fixes it; I believe the problem lies
in the XML parser itself. As Wayne so aptly put it, "We fear change."

The two quick fixes I've employed to get around this until a better solution
presents itself are either &nbsp; entities after every anchor, or using CSS
to add spacing. [The second option is, in my opinion, much cleaner.]

  in my template:
  <a class="navbar" href="1">Home</a>
  <a class="navbar" href="2">Stuff</a>
  in my CSS:
  a.navbar { padding: 0px 5px 0px 5px; }

I'm not entirely happy with this, but it makes my pages look nice until any
issues with the XML parser get hammered out.

As far as chomping off docstrings, that's something you might want to add to
the tracker <http://www.twistedmatrix.com/bugs>. I see it happening on my
site as well, and Woven probably shouldn't be doing that.

Alex Levy
WWW: http://mesozoic.geecs.org
"Never let your sense of morals prevent you from doing what is right."
 -- Salvor Hardin, Isaac Asimov's _Foundation_

More information about the Twisted-Python mailing list