[Zope] METAL & parameters?

Jennifer A. Drummond jenn@io.com
Fri, 22 Feb 2002 16:22:58 -0600


> 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