martin f krafft wrote at 2003-2-1 21:03 +0100:
I have the following bit of TALES code in one of my pages. It doesn't occur just once, it occurs multiple times, with the parameters at AAAAAA, BBBBBBB, CCCCCCC, and DDDDDDD changing between each occurrence:
<tr tal:define="field form/AAAAAA; value request/form/field_BBBBBB|person/CCCCCCCCC|nothing"> <td class="label" tal:content="field/title">Label</td> <td class="field"> <input type="text" tal:replace="structure python:field.render(value)" /> <span class="formerror" tal:on-error="string:" tal:condition="showerrors" tal:content="structure python:formErrors['DDDDDD']"> Error </span> </td> </tr>
Because everything else is the same, I'd like to factor the code out. I was thinking about macros, but using macros within tal statements or tags produces compilation errors.
How else can I factor the above out and parametrise it? You make the macro you like and have your parameters as "unbound" variables in it. You then use:
<metal:params tal:define="... Definitions for your unbound variables ..."> <tr metal:use-macro="...full reference to your macro..."> </tr> </metal:params> The "metal:params" above is inessential; it is just a tag that is not rendered (because it is in a "tal/metal" namespace). Any other tag would do as well. I use "metal:params" for documentation purposes, because I define here parameters for macros... Inside your macro definition, you might want to use variable interpolation in "path" expressions. Its syntax is: "<path>/?varname". This substitutes the content of "variable" for "?variable". Dieter