[ZPT] Re: Passing Parameters to Templates

Philip Kilner phil at xfr.co.uk
Thu Oct 28 10:51:24 EDT 2004


Hi Tonico.

Thanks very much - worked first time!

For posterity:_

Tonico Strasser wrote:
>> I've written a macro to do some formatting of a value - but how can I 
>> pass the value to the template in my "metal:use-macro" statement?
>>
> 
> If you have a template 'my_macros' ...
> ---
> <div metal:define-macro="x">
>   <div tal:content="y"/>
> </div>
> ---
> 

My macro is:-

<div metal:define-macro="ukdatetime_or_null">
     <div tal:condition="RawDateTime">
         <span tal:replace="python: RawDateTime.strftime('%d/%m/%Y 
%H:%M')">Date</span>
     </div>
</div>

> ... and you use the above macro in another template ...
> ---
> <div tal:define="y string:test" tal:omit-tag="">
>   <div metal:use-macro="here/my_macros/macros/x"/>
> </div>
> ---
> 

...and the useage is: -

<td tal:define="RawDateTime result/Created"
     bgcolor="#F5F5F5" align="left">
     <span metal:use-macro="here/formats_pt/macros/ukdatetime_or_null"/> 
         </td>


> ... the output will be:
> ---
> <div>
>   <div>test</div>
> </div>
> ---
> 

So the output is  "28/10/2004 14:15"

> You don't need to pass the value to the macro.
> 

Fab - I was trying too hard!

I have a large number of places where I need to see if the SQL Server 
timestamp field is present in "result" - and only try to format it if it 
exists. /Just/ enough of a PITA (given the number of places I must do 
this) to factor out into a macro!


-- 

Regards,

PhilK

Email: phil at xfr.co.uk / Voicemail & Facsimile: 07092 070518

"Work as if you lived in the early days of a better nation." - Alasdair Gray


More information about the ZPT mailing list