Getting an object from it's id
Hi, This is obviously a newbie question. I have several places in my dtml code where in order to obtain an object I loop through objects of that kind and make a test to determine if each objects id matches the id I picked up from a form. This is the only way I could get it to work; but I feel there must be a better, more efficient way. Here is an example: <dtml-in expr="objectValues('Folder')"> <dtml-if "myId==id"> Once I get a match I then proceed to use it in subsequent code, such as a <dtml with id> and it works since id now refers to the object I'm interested in. Is there a Zope API method that will allow me to get an object from it's id, or can I write a simple Python method to do it, or am I missing something even more obvious? Thanks Geoff Armstrong
On Tue, 23 Jan 2001, Gale wrote:
<dtml-in expr="objectValues('Folder')"> <dtml-if "myId==id">
You don't need to "get" the object, as the object is already on top of the namespace stack (dtml-in put the object there on every iteration). Just use the object's attributes (id, after all, is just yet another attribute). Oleg. ---- Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
on 1/23/01 12:34 PM, Oleg Broytmann at phd@phd.pp.ru wrote:
On Tue, 23 Jan 2001, Gale wrote:
<dtml-in expr="objectValues('Folder')"> <dtml-if "myId==id">
What I'm trying to do here is avoid using <dtml-in, since in order for it to put the object in the namespace it has to iterate over all of them (there could be any number). All I have at the beginning is the name of the object in the REQUEST part of the namespace as picked up from a form. My method is the action to that form. In DTML there doesn't seem to be a way of saying, "I have this string with the name of an object, now fetch me the object." Geoff
You don't need to "get" the object, as the object is already on top of the namespace stack (dtml-in put the object there on every iteration). Just use the object's attributes (id, after all, is just yet another attribute).
Oleg. ---- Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
+-------[ Geoff Armstrong ]---------------------- | on 1/23/01 12:34 PM, Oleg Broytmann at phd@phd.pp.ru wrote: | | In DTML there doesn't seem to be a way of saying, "I have this string with | the name of an object, now fetch me the object." if objectname is actually a variable with the name... <dtml-with "_.getitem(objectname,0)"> or <dtml-call "REQUEST.set('fooobject',_.getitem(objectname,0))"> to get the object into a variable to pass or manipulate. -- Totally Holistic Enterprises Internet| P:+61 7 3870 0066 | Andrew Milton The Internet (Aust) Pty Ltd | F:+61 7 3870 4477 | ACN: 082 081 472 ABN: 83 082 081 472 | M:+61 416 022 411 | Carpe Daemon PO Box 837 Indooroopilly QLD 4068 |akm@theinternet.com.au|
Speaking of getitem...
if objectname is actually a variable with the name...
<dtml-with "_.getitem(objectname,0)">
Can someone explain this from the ZQR? getitem(name,flag) Lookup a name in the namespace. If the value is callable and the flag is true, then the result of calling the value is returned, otherwise the value is returned. flag defaults to false. I use getitem() quite alot and never set the flag (therefore it's false). It works and does what I want, but the ZQR entry doesn't seem to support that and I have seen it given as an example with the flag set true with what appears to me to be the same results. Thanks, -- Tim Cook, President -- Free Practice Management,Inc. | http://www.FreePM.com Office: (901) 884-4126 "Liberty has never come from the government." - Woodrow Wilson
Tim Cook wrote:
I use getitem() quite alot and never set the flag (therefore it's false). It works and does what I want, but the ZQR entry doesn't seem to support that and I have seen it given as an example with the flag set true with what appears to me to be the same results.
...it can quite often be, but try this: if mymethod is a DTML method: _.getitem('mymethod') will return a DTML Method object. _.getitem('mymethod',1) will return a lump of text. HTH, Chris
Chris Withers wrote:
Tim Cook wrote:
I use getitem() quite alot and never set the flag (therefore it's false). It works and does what I want, but the ZQR entry doesn't seem to support that and I have seen it given as an example with the flag set true with what appears to me to be the same results.
...it can quite often be, but try this:
if mymethod is a DTML method:
_.getitem('mymethod') will return a DTML Method object.
_.getitem('mymethod',1) will return a lump of text.
It does, thanks. See, simple answers for simple minds. <s> -- Tim Cook, President -- Free Practice Management,Inc. | http://www.FreePM.com Office: (901) 884-4126 "Liberty has never come from the government." - Woodrow Wilson
Geoff Armstrong wrote:
What I'm trying to do here is avoid using <dtml-in, since in order for it to put the object in the namespace it has to iterate over all of them (there could be any number).
All I have at the beginning is the name of the object in the REQUEST part of the namespace as picked up from a form. My method is the action to that form.
In DTML there doesn't seem to be a way of saying, "I have this string with the name of an object, now fetch me the object."
Hi Geoff, Try using _.getitem(), as in: <dtml-let myObject="_.getitem(myId)"> ... <dtml-call myObject.foo> ... </dtml-let> or probably: <dtml-let myObject="_.getitem(REQUEST['myId'])"> If you aren't sure of the existance of the object, wrap the above in: <dtml-if "_.hasattr(this(), REQUEST['myId'])"> </dtml-if> Ivan
on 1/23/01 1:20 PM, Ivan Cornell at ivan.cornell@framestore.co.uk wrote: Hi Ivan, Thanks. The "_.getitem(myId)" works well. I've made a note of the "hasattr" trick as well for future reference. Geoff
Geoff Armstrong wrote:
What I'm trying to do here is avoid using <dtml-in, since in order for it to put the object in the namespace it has to iterate over all of them (there could be any number).
All I have at the beginning is the name of the object in the REQUEST part of the namespace as picked up from a form. My method is the action to that form.
In DTML there doesn't seem to be a way of saying, "I have this string with the name of an object, now fetch me the object."
Hi Geoff,
Try using _.getitem(), as in: <dtml-let myObject="_.getitem(myId)"> ... <dtml-call myObject.foo> ... </dtml-let> or probably: <dtml-let myObject="_.getitem(REQUEST['myId'])">
If you aren't sure of the existance of the object, wrap the above in: <dtml-if "_.hasattr(this(), REQUEST['myId'])"> </dtml-if>
Ivan
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
[geofstro@monaco.mc] | Is there a Zope API method that will allow me to get an object from | it's id, or can I write a simple Python method to do it, or am I | missing something even more obvious? Well, sometimes objectItems or objectValues can be the right ones to use. Other times, (in Python Products at least) you use _getOb() to fetch the object for you. Let's say you have this object structure: /Folder1 /Folder2 If you (in Python code) have Folder1 in the namespace, as self for example, you can say object=self._getOb('Folder2'), and vòila. :) Don't know how you would do that in DTML, though.
on 1/23/01 1:12 PM, Erik Enge at erik+list@esol.no wrote: Thanks; but being new to this could you help me a bit more by providing an example of how to use 'getOb' within <dtml code. I tried testing in; but couldn't get it work. Geoff
[geofstro@monaco.mc]
| Is there a Zope API method that will allow me to get an object from | it's id, or can I write a simple Python method to do it, or am I | missing something even more obvious?
Well, sometimes objectItems or objectValues can be the right ones to use. Other times, (in Python Products at least) you use _getOb() to fetch the object for you.
Let's say you have this object structure:
/Folder1 /Folder2
If you (in Python code) have Folder1 in the namespace, as self for example, you can say object=self._getOb('Folder2'), and vòila. :) Don't know how you would do that in DTML, though.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (8)
-
Andrew Kenneth Milton -
Chris Withers -
Erik Enge -
Gale -
Geoff Armstrong -
Ivan Cornell -
Oleg Broytmann -
Tim Cook