[Zope] Re: getting object reference from an id string?

Alexandre Ratti alex@gabuzomeu.net
Thu, 09 Mar 2000 13:04:15 +0100


Hello Darran,


building on Rik's previous answer, how about this:

   <dtml-call "REQUEST.set('myObjRef', _[desired_id])">

It tested OK with an Image object called "image" and located in the site root:

# Storing the image name in a variable
<dtml-call "REQUEST.set('desired_id','image')">

# Creating an object reference based on the variable
<dtml-call "REQUEST.set('myObjRef', _[desired_id])">

# Displaying the object title and size
<dtml-var "myObjRef.title">
<dtml-var "myObjRef.getSize()">

# Rendering the object if it's renderable. An image is.
<dtml-var myObjRef>

If it's not renderable, you'll end up with something like "<Folder instance 
at 01368880>" in the HTML page source code.

Does it work for you? If yes, you may want to add a FAQ Question and it's 
Answer on zdp.zope.org (hint, hint :-).


Cheers.

Alexandre


At 04:55 08/03/2000 -0800, you wrote:
>From: "Darran Edmundson" <Darran.Edmundson@anu.edu.au>
>To: "zope" <zope@zope.org>
>Subject: [Zope] getting object reference from an id string?
>Date: Wed, 8 Mar 2000 23:24:46 +1100
>charset="iso-8859-1"
>
>Say one has an object reference and knows the
>string id of a child node.  From this info, we
>want an object reference to the named child.
>
>I'd like someone to prove me wrong that there
>is not a one-liner to accomplish this seemingly
>trivial task.  I've had to resort to (off the
>top of my head) code like:
>
><dtml-in "objectValues(['child meta-type'])">
>    <dtml-with sequence-item>
>       <dtml-if "id == desired_id">
>           <dtml-call "REQUEST.set('desired_ref',_['sequence-item'])">
>       </dtml-if>
>    </dtml-with>
></dtml-in>
>
>I'm really looking for a solution that doesn't
>require looping.  Since Zope enforces unique id's
>at a given level, the name should be sufficient
>to resolve the child object.
>
>Darran.