[ZPT] Using Script to Select Macro

Jeff Peterson jpeterso at rangebroadband.com
Wed Aug 4 11:20:46 EDT 2004


 wrote:
> Any help or pointers to reference materials would be most appreciated
> - I am attempting to use a Python script to return the path to a macro
> that I want to use in a given page. I'm passing the 'linktype'
> argument when I call the template from a script - hence the
> 'options/linktype'. Here's the error I get:
>
> Error Type: NameError
> Error Value: name 'linkme' is not defined
>
> <p tal:define="linkme options/linktype"
> metal:use-macro="python:here.getNavLinks(linktype=linkme)">test</p>
>
> The Python script simply returns the string path to the macro.  If I
> change the metal:use-macro to tal:content then the returned string
> appears as text on the page but I can't seem to get the macro to be
> included.  This seems straightforward but it isn't working.
>
> When I used this:
> <div tal:define="linkme options/linktype">
>     <p tal:content="python:here.getNavLinks(linkme)">linktype</p>
> - the path returned from the Python script appeared fine - then after
> a bit more struggle and testing I started seeing this error from the
> markup above: Error Type: NameError
> Error Value: global name 'here' is not defined
>
> Here's the Python script getNavLinks - simple stuff.
>
> from Products.PythonScripts.standard import html_quote
> request = container.REQUEST
> RESPONSE =  request.RESPONSE
>
> if (linktype=="User"):
>     return "here/navlinks/macros/userlinks"
> elif (linktype=="Silo"):
>     return "here/navlinks/macros/silolinks"
> elif (linktype=="BuildType"):
>     return "here/navlinks/macros/buildtypelinks"
>
>
> Thanks,
>
> Doug
> _______________________________________________
> ZPT mailing list
> ZPT at zope.org
> http://mail.zope.org/mailman/listinfo/zpt

Doug,

You have a two problem I can see:

1) "metal:use-macro="python:here.getNavLinks(linktype=linkme)" doesn't work
that I can see.  I am not sure, but I doubt python is allowed there. Even if
it was, once it rendered the python script the cooking would be done and the
path would be ignored.  It would look like this when you viewed source: <p
metal:use-macro="here/navlinks/macros/userlinks">test</p> rather than the
macro you'd expect.  But, as I said I can't make that work, period, even for
simple cases.

2) Even if (1) worked, it will render that macro _before_ doing the tal so
defining the options and expecting to be able to use them is futile.

Unless there is some Zope magic that I don't know about (and there
frequently is) you can't do this, this way.

You could (I think) put all the nav choices in the macro using
"tal:condition" based on the linktype then that code would be macro'ed in
and conditionally displayed based on the options.

Regards,

Jeffrey D. Peterson
Webmaster
Range TV Cable & Broadband
jpeterso at rangebroadband.com



More information about the ZPT mailing list