[Twisted-web] Alternative to <nevow:attr>

Andrew Bennetts andrew-twisted at puzzling.org
Wed Oct 20 06:01:36 MDT 2004


On Wed, Oct 20, 2004 at 10:26:36AM +0100, Matt Goodall wrote:
> On Tue, 2004-10-19 at 23:06 +0200, Andrew Bennetts wrote:
[...]
> 
> 
> >     <ul tal:repeat="linkinfo context/links">
> >       <li><a href="#" tal:attributes="href linkinfo/link; label linkinfo/label"/></li>
> >     </ul>
> > 
> > Although I don't totally understand why mg's attr.tac has a nevow:slot tag
> > for label and not link, as far as I can tell the ZPT I give here is
> > equivalent in concept.  Explanations and/or corrections welcome!
> 
> The attribute renderer in attr.tac example updates updates from values
> in slots, i.e. href is set to the href slot and alt (yeah, it should be
> label;-)) is set to the label slot. The slot *inside* the <a>...</a> is
> actually nothing to do with the attribute renderer but ensures that the
> label also appears inside the element.

Oh, right.  I get it now, duh :)

> I don't know ZPT well enough to know if your example does exactly the
> same (I can't see how it could) but that really doesn't matter for this
> discussion anyway.

My ZPT to be equivalent should be:

    <ul tal:repeat="linkinfo context/links">
      <li><a href="#" tal:attributes="href linkinfo/link; alt linkinfo/label"
             tal:content="linkinfo/label"/>Label will go here</li>
    </ul>

I think it's a shame that no-one seems to know both ZPT and Nevow, as I
suspect there may be something they can learn from each other, as they have
some similar goals.  I'd like to learn Nevow (although I'm really no ZPT
expert), but time is always the problem...

> I should point out that most of the time, I use the <n:attr ...
> ><n:slot ...></n:attr> syntax because it's always there and it's more
> flexible. I wrote the attr.tac more as an example of how one might
> implement something like this using a renderer although it does have the
> benefit of being compact for really simple stuff.
> 
> > 
> > Of course, my ZPT examples requires that the code would require there to be
> > a linkinfo class, e.g.
> > 
> >     class LinkInfo:
> >         def __init__(self, link, label):
> >             self.link = link
> >             self.label = label
> > 
> > And context/links would just be a list of these.  I don't see this as being
> > any more complex than what attr.tac does, though.
> 
> This is probably the right way with Nevow too. The difference with Nevow
> is that Nevow refuses to look inside objects it doesn't understand by
> default so it would never use getattr unless explicitly told to.

I don't know exactly where this happens in Zope 3, but it won't by default
either.  So, the code would look more like this:

    from zope.interface import Interface, Attribute, implements

    class ILinkInfo(Interface):
        link = Attribute('URI that a link points to')
        label = Attribute('Short description of the link')

    class LinkInfo:
        implements(ILinkInfo)
        ...

And then usually in a ZCML file say that ZPT (or really the publisher in
general) is allowed to access attributes and methods defined in ILinkInfo.

> You can tell Nevow how to look inside objects by registering a
> inevow.IContainer adapter. We've talked about providing a IContainer
> adapter that does getattr magic in Nevow but it would be up to the
> developer to register the adapter for specific types.

I see.  This seems essentially equivalent to the ZPT way -- registering an
adapter from ILinkInfo to IContainer.  Zope does things via security
declarations because there's a desire to allow relatively untrusted sources
to write page templates, I guess.  There's more in common here than
different, though :)

> > If nothing else, I hope this discussion can serve as the basis for a "Nevow for
> > ZPT users" tutorial.
> 
> I guess I should learn ZPT properly sometime, just to get another angle
> on all this.

And I should learn Nevow  :)

-Andrew.




More information about the Twisted-web mailing list