[Zope] manage_addDTMLDocument

Dieter Maurer dieter@handshake.de
Thu, 8 Mar 2001 21:10:20 +0100 (CET)


nuno writes:
 > <dtml-var location>  -> has the string 'PARENTS[0].DIR1.DIR2' which is the
 > place where i desire to create the document.then i try this:
 > <dtml-call
 > "_[location].manage_addDTMLDocument(id=pagename,title=pagetitle,file=page)">
This is an FAQ.
I answer it over and over again...

Please read the mailing list archives (searchable) or

  URL:http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html

If you have a string S (in your case 'PARENTS[0].DIR1.DIR2')
and you look it up in the namespace "_" then the
string is *NOT* treated as an expression but
used atomically (literally) as the key in the namespace
mapping. Thus, you look for on object with name
"PARENTS[0].DIR1.DIR2". This name contains many funny characters,
two '.', '[' and ']'.
You want this funny characters to be treated as operators.
Python's "eval" would do this, but it is not exposed to DTML
for security reasons.

What are your options?

 1. inside an external method, you can use Python's eval

    External Method:
       def extmethod(expr,_): return eval(expr,_)

    Called from DTML:

       "....extmethod(location,_)...."

 2. you can try to use "restrictedTraverse" to map
    strings (or sequences of strings) to objects.

    In your case, "restrictedTraverse" can not
    evaluate "PARENTS[0]". But "DIR1.DIR2"
    can be handled:

	PARENTS[0].restrictedTraverse(_.split(location,'.'))


Dieter