[Zope] dtml-with & misbehaving strings
Chris McDonough
chrism@digicool.com
Wed, 02 Feb 2000 02:25:56 -0500
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