Hi, I have a METAL define which just draws a pretty little box with a title and content: <div metal:define-macro="box"> <table bgcolor="#000090" border="0" cellpadding="1" cellspacing="0" width="200" align="center"> <tr><td> <table border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF" width="200"> <tr><td bgcolor="#000090" tal:attributes="bgcolor string:#000090"> <font size="-1" face="Helvetica,Arial,sans-serif" color="#ffffff"> <strong><div metal:define-slot="title">The Title</div></strong> </font></td></tr> <tr><td> <font class="element"> <div metal:define-slot="body">Contents goes here ...</div></font></td></tr> </table></td></tr> </table> </div> What I would like to be able to do, is set the "bgcolor" attribute dynamically when using this macro. It is not obvious to be whether this is possible or not. The tal:attribute clause is invalid as I have nested tags. Is there any xml-ified version of the good old fashioned C #define functionality for this situation?? Cheers, Alan _________________________________________________________________ Send and receive Hotmail on your mobile device: http://mobile.msn.com
I have a METAL define which just draws a pretty little box with a title and content:
[snip]
What I would like to be able to do, is set the "bgcolor" attribute dynamically when using this macro.
It is not obvious to be whether this is possible or not. The tal:attribute clause is invalid as I have nested tags. Is there any xml-ified version of the good old fashioned C #define functionality for this situation??
Cheers, Alan
C is way over my head, but I think you can do this just fine with METAL. Macros have access to variables that are set around them in the calling template, so this should work. In the calling template, set a variable like "passedcolor": <span tal:define="passedcolor string:FF0000"> <div metal:use-macro="here/box/macros/box"> <span metal:fill-slot="title">Passed-In Title</span> <span metal:fill-slot="body">Passed-In Content</span> </div> </span> ...and then make your macro use that whereever it specifies bgcolor. The implementation below has a default, so it won't error in the absence of the passed variable. <div metal:define-macro="box"> <table tal:define="defaultcolor string:000090; boxcolor passedcolor | defaultcolor" tal:attributes="bgcolor boxcolor" bgcolor="#000090" border="0" cellpadding="1" cellspacing="0" width="200" align="center"> <tr><td> <table border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF" width="200"> <tr> <td bgcolor="#000090" tal:attributes="bgcolor boxcolor"> <font size="-1" face="Helvetica,Arial,sans-serif" color="#ffffff"> <strong><div metal:define-slot="title">The Title</div></strong> </font></td></tr> <tr><td> <font class="element"> <div metal:define-slot="body">Contents goes here...</div> </font></td></tr> </table> </td></tr> </table> </div> If you were having trouble with the tal:attribute part, I don't think it was due to tag nesting. Give this a try! =-=-> Jenn Drummond // jenn@io.com
alan milligan writes:
I have a METAL define which just draws a pretty little box with a title and content:
<div metal:define-macro="box"> ... <table border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF" ... </div>
What I would like to be able to do, is set the "bgcolor" attribute dynamically when using this macro. It's a big weakness (I my view) that ZPT macros only support slots as parameters. Slots are restricted to (HTML/XML) sub-elements, but often, we need way to have attribute values are parameters (as in your case).
I work around this weakness by using unbound variables inside the macro definition: <... metal:define-macro=...> <table border="0" ... bgcolor="#FFFFFF" tal:attributes="bgcolor box_bgcolor" > </...> When the macro is used, the caller uses something like: <tal:div tal:define="box_bgcolor string:#000000"> .... <... metal:use-macro=...> </...> .... </tal:div> This works but the disadvantages are obvious: * Unbound variables are very difficult to spot. Combined with a lacking documentation facility for macros, this make reuse and maintenance quite difficult. * At least the template gets artificial elements (e.g. the "tal:div" above). It clutters the ZPT (not a big problem, just a nuisance). I would very much like to see explicit parameter passing for definitions... Dieter
participants (3)
-
alan milligan -
Dieter Maurer -
Jennifer A. Drummond