[ZPT] METAL HowTo (was Filling slots in METAL macros...)

Tony McDonald tony.mcdonald@ncl.ac.uk
Tue, 31 Jul 2001 13:23:05 +0100


On 27/7/01 4:37 pm, "Evan Simpson" <evan@zope.com> wrote:

> 
> I really need to add METAL to the ZPT tutorial, but in the meantime
> here's a quick rundown:
> 

Thanks again for the help Evan. I have spent all weekend going over METAL
and how I might be able to use it, incorporating your advice from this mail
as well as reading as much as I could get my hands on. I'm in the process of
writing up a HowTo on 'METAL for beginners' that is an attempt to give
something back. It's at
http://www.zope.org/Members/tone/METAL but it may be in the review loop at
the moment. It's certainly not comprehensive, but might help some people
out.


> Macros are a way to share chunks of presentation, and have the shared
> stuff appear inline in the template.  You don't use them for plugging in
> data values; That's what tal:replace and tal:content are for.
> 

Ok, got that.

> Whenever a template that uses macros is rendered, all of the macros are
> plugged in just before TAL processing.

Ok, so all macro content is expanded out, and then TAL processing takes
over. Got that.

> If you edited the page and
> removed all of the METAL attributes, *it would make no difference* as
> long as you didn't try to change any of the macro definitions.

The page using the macro? I tend not to use the 'Expand Macros...' tag as
there seems no way to get back to the cleaner version of my page template.

> TAL 
> statements don't know where they "come from", they only care about the
> template they're being rendered in.  This means that "here", "template",
> "options", and all other variables are exactly the same inside macros as
> out. 

Ok got that.

> You can tal:define a variable, then use it in an enclosed macro.
> You can tal:define a global variable in a macro, then use it below the
> macro.
>

That doesn't seem right to me, suppose I have in a page template that calls
a macro definition this;

<html
  metal:use-macro="here/standard_look_and_feel/macros/master"
>
...

<div tal:define="oboxtitle string:Time Table Information">
<div metal:use-macro="here/obox/macros/obox">
<p>this is in the obox macro - this text won't get shown - it is not in the
context of 'somecontent'</p>
<div metal:fill-slot="somecontent">Time table information for <span
tal:replace="user/getUserName">username</span>
</div>
</div>

Where the macro 'obox' has something like this in it;

<div metal:define-macro="obox">
<table>
<tr>
<td><b tal:content="oboxtitle">a default title for Obox</b></td>

...
<div metal:define-slot="somecontent"> ... </div>
</div>

Why can't I combine the tal:define into the metal:use-macro attribute like
so;

<div 
tal:define="oboxtitle string:Time Table Information"
metal:use-macro="here/obox/macros/obox">

That seems more intuitive to me, keeping the variables with the tag you're
replacing.

A consequence of this appears to be that I need to call my 'loadup' python
scripts in the <body> tag of the macro 'standard_look_and_feel' like so;

<body define-macro="body" tal:define="global data here/loadup"

And in my Page Template that uses the macro template I have this;
<body metal:fill-slot="body" >

So instead of loading up my variables using a python script in the page
template I want to use them in, I apparently need to do it in the macro
*template*. Is this right?

> You only need to use slots if you want to be able to override parts of a
> macro in templates that use it.  To do this, give a tag in the macro
> definition a slot name with metal:define-slot.  When you use this macro,
> the slot contents will be used normally along with the rest of the
> macro, unless you add a metal:fill-slot statement with the slot's name.
>  A metal:fill-slot statement tag completely replaces the slot tag from
> the macro definition, including the tag name and any attributes.
> 

That's great Evan, I've added this to the HowTo. It's now awaiting review.

> A single tag cannot have more than one METAL attribute on it.

Ok, got that (I completely forgot that we're also dealing with HTML here and
you can't have two attributes with the same name).

I guess my main sticking point is on passing variables to the macros - if
anyone can help out, I'd appreciate it. The learning curve is pretty steep
for me at the moment.

The URL of the HowTo is
http://www.zope.org/Members/tone/METAL

Cheers,
Tone.
-- 
Dr Tony McDonald,  Assistant Director, FMCC, http://www.fmcc.org.uk/
The Medical School, Newcastle University Tel: +44 191 243 6140
A Zope list for UK HE/FE  http://www.fmcc.org.uk/mailman/listinfo/zope