dtml-with & misbehaving strings
Hi everyone, I have a folder with a property. In a method within this folder I try the following: <dtml-with "_[myproperty]"> grab some stuff </dtml-with> where myproperty is a string equal to Games.Whatup Both 'Games' and 'Whatup' are objects. So I am trying to drill down my directory and stack the namespace via the with tag, but I get the following message: Error Type: KeyError Error Value: Games.Whatup I want it to to do the following (which works fine): <dtml-with "Games.Whatup"> grab some stuff </dtml-with> Am i missing something here? Thanks for any help and hints in advance. Thanks, Thomas
I would try "_[_['myproperty']]" but without much confidence. These indirect references are very confusing. -- Loren
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Thomas Sent: Tuesday, February 01, 2000 09:29 To: zope@zope.org Subject: [Zope] dtml-with & misbehaving strings
Hi everyone,
I have a folder with a property. In a method within this folder I try the following:
<dtml-with "_[myproperty]"> grab some stuff </dtml-with>
where myproperty is a string equal to Games.Whatup
Both 'Games' and 'Whatup' are objects. So I am trying to drill down my directory and stack the namespace via the with tag, but I get the following message:
Error Type: KeyError Error Value: Games.Whatup
I want it to to do the following (which works fine):
<dtml-with "Games.Whatup"> grab some stuff </dtml-with>
Am i missing something here? Thanks for any help and hints in advance.
Thanks, Thomas
_______________________________________________ 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 )
Thomas wrote:
Hi everyone,
I have a folder with a property. In a method within this folder I try the following:
<dtml-with "_[myproperty]"> grab some stuff </dtml-with>
where myproperty is a string equal to Games.Whatup
Both 'Games' and 'Whatup' are objects. So I am trying to drill down my directory and stack the namespace via the with tag, but I get the following message:
Error Type: KeyError Error Value: Games.Whatup
I want it to to do the following (which works fine):
<dtml-with "Games.Whatup"> grab some stuff </dtml-with>
Am i missing something here? Thanks for any help and hints in advance.
Yup. The namespace lookup expression: "_[name]" takes a single name, but you have given it a string containing two names. I suggest that "Games.Whatup" is really a path expression. (Note that dots are legal in Zope names and therefore not a good choice for path separators, but that's beside the point.) There isn't a general way, in DTML currently to take a string containing a path and convert it two an object. (If there was, it would use '/'s as path sperators, as in "Games/Whatup".) Now, suppose your property was defined with "lines" or "tokens", which is a list of strings, and that you know that it always contained a two-part path, then you could use something like: <dtml-with "_.getattr(_[myproperty[0]], myproperty[1])"> This gets the name from the namespace corresponding to the first element if myproperty and then gets an attribute from the resulting object using the second element. Hope this helps. Jim -- Jim Fulton mailto:jim@digicool.com Technical Director (888) 344-4332 Python Powered! Digital Creations http://www.digicool.com http://www.python.org Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email address may not be added to any commercial mail list with out my permission. Violation of my privacy with advertising or SPAM will result in a suit for a MINIMUM of $500 damages/incident, $1500 for repeats.
Evan Simpson came up with a pretty neat way to deal with this back in December... A quote from his mailing list post about it follows: ---------- quote from evan ------------- Subject: [Zope] String to object ID 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')"> Jim Fulton wrote:
Thomas wrote:
Hi everyone,
I have a folder with a property. In a method within this folder I try the following:
<dtml-with "_[myproperty]"> grab some stuff </dtml-with>
where myproperty is a string equal to Games.Whatup
Both 'Games' and 'Whatup' are objects. So I am trying to drill down my directory and stack the namespace via the with tag, but I get the following message:
Error Type: KeyError Error Value: Games.Whatup
I want it to to do the following (which works fine):
<dtml-with "Games.Whatup"> grab some stuff </dtml-with>
Am i missing something here? Thanks for any help and hints in advance.
Yup. The namespace lookup expression: "_[name]" takes a single name, but you have given it a string containing two names. I suggest that "Games.Whatup" is really a path expression. (Note that dots are legal in Zope names and therefore not a good choice for path separators, but that's beside the point.)
There isn't a general way, in DTML currently to take a string containing a path and convert it two an object. (If there was, it would use '/'s as path sperators, as in "Games/Whatup".)
Now, suppose your property was defined with "lines" or "tokens", which is a list of strings, and that you know that it always contained a two-part path, then you could use something like:
<dtml-with "_.getattr(_[myproperty[0]], myproperty[1])">
This gets the name from the namespace corresponding to the first element if myproperty and then gets an attribute from the resulting object using the second element.
Hope this helps.
Jim
-- Chris McDonough Digital Creations, Inc. Zope - http://www.zope.org
participants (4)
-
Chris McDonough -
Jim Fulton -
Loren Stafford -
Thomas