Dear all, I'm trying to get a Zope Page Template to use a macro which is passed a few parameters that need to be accessed by Python. I was under the impression that "define-slot" and "fill-slot" where the appropriate ways of passing parameters, but I'm really struggling to use these slots from within Python expressions in the macro. My macro looks like so: <span metal:define-macro="pullActivityReport"> <tr tal:repeat="update python:container.get_updates_made(templateid=MYTEMPLATEIDSLOT)"> <td><span tal:replace="update/topicname">Topic Name</span></td> </tr> </span> While my main page looks like: <table> <span metal:use-macro="container/macroDefs/macros/pullActivityReport"> <span metal:fill-slot="MYTEMPLATEIDSLOT">22</span> </span> </table> Is such an amazing feat possible? I am currently using "tal:define global" to pass parameters but it seems like a less than desirable solution (global scope, etc...) Regards, Bryn
Bryn Thomas wrote at 2004-10-19 01:17 +0200:
... I'm trying to get a Zope Page Template to use a macro which is passed a few parameters that need to be accessed by Python. I was under the impression that "define-slot" and "fill-slot" where the appropriate ways of passing parameters, but I'm really struggling to use these slots from within Python expressions in the macro.
"slot"s are paramters but of a different type (they provide content not variables).
My macro looks like so:
<span metal:define-macro="pullActivityReport"> <tr tal:repeat="update python:container.get_updates_made(templateid=MYTEMPLATEIDSLOT)"> <td><span tal:replace="update/topicname">Topic Name</span></td> </tr> </span>
While my main page looks like:
<table> <span metal:use-macro="container/macroDefs/macros/pullActivityReport"> <span metal:fill-slot="MYTEMPLATEIDSLOT">22</span> </span> </table>
Use instead: <table> <metal:params tal:define="MYTEMPLATEIDSLOT python:22"> <span metal:use-macro="..." /> </metal:params> </table> Note that "metal:XXX" tags are silently omitted. Aside from this, there is nothing special with "metal:params", you can use any other tag you like for documentation purposes. -- Dieter
Thanks for this answer! I never realised that the scope of the local metal:params extended all the way within the tag block. I see by your example that you use <metal:params tal:define="variablename python:22"> to set variablename to be an integer of 22. I've used the Zope "variablename string:mystring" syntax to set a variable to be a string, and I then tried several combinations of things like "variablename int:myint" and "variablename integer:myint" etc etc, with no success. Assumedly then you use the syntax "variablename python:mynonstringvariable" to set variables of pretty much any non-string type. Regards, Bryn -----Original Message----- From: Dieter Maurer [mailto:dieter@handshake.de] Sent: 19 October 2004 08:15 PM To: Bryn Thomas Cc: zope@zope.org Subject: Re: [Zope] Accessing a macro slot from Python Bryn Thomas wrote at 2004-10-19 01:17 +0200:
... I'm trying to get a Zope Page Template to use a macro which is passed a few parameters that need to be accessed by Python. I was under the impression that "define-slot" and "fill-slot" where the appropriate ways of passing parameters, but I'm really struggling to use these slots from within Python expressions in the macro.
"slot"s are paramters but of a different type (they provide content not variables).
My macro looks like so:
<span metal:define-macro="pullActivityReport"> <tr tal:repeat="update python:container.get_updates_made(templateid=MYTEMPLATEIDSLOT)"> <td><span tal:replace="update/topicname">Topic Name</span></td> </tr> </span>
While my main page looks like:
<table> <span metal:use-macro="container/macroDefs/macros/pullActivityReport"> <span metal:fill-slot="MYTEMPLATEIDSLOT">22</span> </span> </table>
Use instead: <table> <metal:params tal:define="MYTEMPLATEIDSLOT python:22"> <span metal:use-macro="..." /> </metal:params> </table> Note that "metal:XXX" tags are silently omitted. Aside from this, there is nothing special with "metal:params", you can use any other tag you like for documentation purposes. -- Dieter
Bryn Thomas wrote at 2004-10-19 21:39 +0200:
... I never realised that the scope of the local metal:params extended all the way within the tag block.
This is a weakness of "metal". In fact, metal macros should have explicit arguments, in addition to the content arguments named "slot"s.
I see by your example that you use <metal:params tal:define="variablename python:22"> to set variablename to be an integer of 22. I've used the Zope "variablename string:mystring" syntax to set a variable to be a string, and I then tried several combinations of things like "variablename int:myint" and "variablename integer:myint" etc etc, with no success. Assumedly then you use the syntax "variablename python:mynonstringvariable" to set variables of pretty much any non-string type.
Yes. Reading the "tal/tales" description in the Zope Book (2.6 or 2.7 edition, online) should help you to get an understanding of the possibilities of "tal/tales". -- Dieter
participants (2)
-
Bryn Thomas -
Dieter Maurer