[Zope] Flexible path generation for metal:use-macro with Python (KeyError)

Clemens Robbenhaar zope@zope.org
Wed, 4 Dec 2002 01:19:23 +0100


 [...]
 > Now, I would like to generate this sequence of alternative paths by a 
 > Python script to have full flexibility. Therefore I tried the following 
 > approach:
 > 
 > Create a Python "getMacroPath" script that takes the parameter "macroname" 
 > and looks like this:
 >  return 'here/content_macro_main.html/macros/' + macroname
 > 
 > (Of course, the script will finally generate many paths separated by "|")
 > 
 > In the ZOPE Page Template mydoc.html I use:
 > <span tal:omit-tag="" tal:define="macropath python:here.getMacroPath('main')">
 >  <span metal:use-macro="here/?macropath">
 >    <!-- main slot -->
 >    <H2>Welcome</H2>
 >    <P>We welcome you to our website.</P>
 >  </span>  
 > </span>

 The expansion of the'macropath' variable does not lead to something You
want to have. If I would be a TALES-interpreter, I would read something
approximately like:
 
 metal:use-macro="here/here/content_macro_main.html/macros/main"

and as 'here' does not have such a thing like
'here/content_macro_main.html/macros/main', this generates the error
message below.


 Instead You could pass the string returned by the script through the
"path(...)" helper function, which returns what You want.

 The following snippet works at least for me:
 
  <span tal:omit-tag="" tal:define="macropath python:path(here.getMacroPath('main'))" >
     <span metal:use-macro="macropath"/>
  </span>

 hope it works for You, too ;)

Cheers,
Clemens
 


 > 
 > The Python script seems to work correctly, but ZOPE complains with 
 > the error:
 >  Error Type: KeyError
 >  Error Value: here/content_macro_main.html/macros/main
 > 
 > However, I don't see why METAL and/or ZOPE cannot deal with a TAL variable 
 > that has the correct value when it can deal with the same value as a 
 > string itself. Any ideas?
 > 
 > 
 > Regards,
 > 
 >  Thomas Duebendorfer
 > 
 > 
 > 
 > 
 > _______________________________________________
 > 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 )