[ZPT] Using PTFiles in Python Products

Michael R. Bernstein webmaven@lvcm.com
11 Jul 2002 17:46:30 -0700


On Wed, 2002-07-10 at 12:31, Michael R. Bernstein wrote:
>
> On Wed, 2002-07-10 at 10:42, Dieter Maurer wrote:
>
> > Michael R. Bernstein writes:
> >  > ...
> >  > How do I access the macros defined in a PTFile defined on an object from
> >  > a PTFile defined on a child object?
> >  > 
> >  > I have an index_html PTFile attribute defined on a container object, and
> >  > another index_html PTFile attribute defined on a subobject. I would like
> >  > to access a macro defined on the parent object's index_html.
> >  > 
> >  > The following does not seem to work:
> >  > <p metal:use-macro="here/../index_html/macros/manage_navbar">Navbar goes
> >  > here</p>
> >  > which raises an 'Undefined' error.
> >
> > You can try to rely on acquisition (this usually works, but may
> > not work with the ubiquitous "index_html").
> 
> Hmm. I can easily shift the macros in question to a 'library' template,
> but this seems somewhat less than ideal.

Unfortunately, this does not seem to be working.

I defined a 'library' PTFile on my container, and calling a macro in it
from another PTFile associated with the container works fine, but does
not when I try calling the same macro from a subobject.

Now, this *may* be due to the fact that the subobjects are stored within
a BTree, and are returned with an acquisition wrapper, but I can't tell.

In any case, calling the macro from a subobject like this does not work:

<p metal:use-macro="here/library/macros/manage_navbar">Navbar goes
here</p>

Any suggestions?

Here is the container's traversal method I'm using to retrieve objects
from the BTree, just in case I've run into an unforeseen side-effect:

    def __bobo_traverse__(self, REQUEST, name):
        """
        Consume trailing URL path and turn into arguments so long as
        they are convertable to ints.  Otherwise, traverse normally with
        collected arguments.  Note that this shadows subobjects with IDs
        that are convertable to ints. Code borrowed from Karl Anderson's
        BlogFace.
        """
        if re.match('^[0-9]+$', name):
            print "object is int-like " + name
            if not REQUEST.has_key('traverse_date'):
               REQUEST['traverse_date'] = []
            (REQUEST['traverse_date']).append(name)
        else:
            # retreive contained object if it exists
            print "object is not int-like " + name
            if name in self.objectIds():
                print "returning contained object " + name
                return getattr(self, name)
            # return attribute (if no matching contained object is
            # found).
            elif hasattr(self, name):
                print "returning object attribute " + name
                return getattr(self, name)
            else:
                # return object stored in BTree
                print "returning object in BTree " + name
                return self.Postings[name].__of__(self)
        print "returning weblog object " + name
        return self


The subobjects (Postings) inherit from SimpleItem and CatalogAware.

Thanks in advance,

Michael Bernstein.