[Zope3-dev] implementing title_or_id

Steve Alexander steve@cat-box.net
Fri, 11 Apr 2003 15:36:14 +0200


Garrett Smith wrote:
> I'm trying to get my head around some of the Zope 3 concepts and have a
> simple problem that I hope will be illustrative for me.
> 
> I want to display something akin to title_or_id for a component. I'm
> envisioning a function that checks the following, in order:
> 
>  - a 'title' attribute for the component
>  - a Dublin Core 'title' annotation
>  - a 'title' annotation from another source
>  - the component id

Look at how the ISized adapters work.

./src/zope/app/interfaces/size.py
./src/zope/app/container/size.py
./src/zope/app/size.py

"Size" is a vague term, and it means different things for different 
objects. To get the "size" of an object, adapt it to ISized and use the 
sizeForSorting() and sizeForDisplay() methods.

These adapters are used when you see an object's size in the contents 
view of a folder in the management interface.


> Either of the following uses would be fine:
> 
>   1 - tal:content="context/title_or_id"
>   2 - tal:content="views/context_utils/title_or_id"

We'll have to wait until the NameSpacesInTemplates proposal is implemented.

http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/NamespacesInTemplates


> I've (partially) implemented the second form, since it seems to follow
> established patterns.
>
> Questions:
> 
> - When "plugging" in new functionality meant to be used only in page
> templates, is the preferred approah to use a view, as in option 2 above?

Why should this be used only in page templates?

This spelling of "getting a view on an object" is deprecated:

 >   2 - tal:content="views/context_utils/title_or_id"

Instead, use this spelling:

 >   2 - tal:content="context/@@/context_utils/title_or_id"

If something is like an adapter, but depends on the request and the 
object, then use a view for it.
In this case, I'm not sure that it depends on the request, only on the 
object. So, use an adapter for it.


> - Is it even possible, per option 1 above, to specify a "wrapper" class
> to add new functionality to components? I understand this is exactly
> what adapters do, but I've only seen this in action in Python modules
> using getAdpater. It's not clear to me how to get this going in TALES,
> ala option 1.

See Shane's proposal, linked above.


> - Given a context, what is the recommended approach for retrieving the
> component ID?

I think you mean the object's name, not the "component id".

See 
http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/NamesKeysAndIds

> I understand that the component's container keeps track of
> this,

Right.

> but it's not clear to me how the component itself can track this
> down.

You can get an objectName adapter, or use an objectName view.

There's a convenience method in zope.app.traversing called "objectName" 
that does this also.

It would be great if these things were documented :-/

--
Steve Alexander