[Zope] String to object ID

Evan Simpson evan@tokenexchange.com
Mon, 6 Dec 1999 09:45:24 -0600


I missed some of the prior discussion, so I hope this isn't irrelevent.

Are you trying to access an object given a string such as 'hie/dee/hoe'?  If
so, try this:

DTML Method FetchObj:
<dtml-let obj="[_]">
  <dtml-in expr="_.string.split(fetchStr, '/')">
    <dtml-call expr="obj.append(obj.pop()[_['sequence-item']])">
  </dtml-in>
  <dtml-return expr="obj[0]">
</dtml-let>

example call:  <dtml-var expr="_.render(FetchObj(_.None, _,
fetchStr='hie/dee/hoe').id)">

Some notes about the above:
o We start with a list containing the global namespace object.
o For each path element, we pop the current object, find the sub-object, and
put it back in the list.
o You could use REQUEST.set instead of the list foolery if you wanted to.
o _.render is necessary since the .id of folders is a string, while that of
methods is a method :-P

If you use PythonMethods, the above can be more simply written as:

PythonMethod FetchObj:
<params>_, fetchStr, attr=None</params>
obj = _
for p in string.split(fetchStr, '/'):
  obj = obj[p]
if attr is None:
  return obj
else:
  return render(_.getattr(obj, attr))


<dtml-var expr="FetchObj(_, 'hie/dee/hoe', 'id')">

----- Original Message -----
From: Goodrichs <nbd95@macconnect.com>
> Well I still don't know how to reference a Zope object from a string, but
I
> have a interim solution for now.