quick search:
 

Smart use of metal macros

Submitted by: peterbe
Last Edited: 2004-10-14

Category: PageTemplates

Average rating is: 4.85 out of 5 (7 ratings)

Description:
In most ZPT objects you start with a HTML tag that defines which metal macro (or header&footer) to use.
If you decide to change that metal macro in the future in will have a bad reference or at least confusion in all other ZPTs.

Please look at my simple hint and discuss if it is that smart that I think it is.


Source (Text):
<!-- The "default" and simple way -->
<html metal:use-macro="here/standard_masterlook/macros/standard">

<!-- A centralized metal macro -->
<html metal:use-macro="here/getHeader">

#
# The getHeader() Python Script method object
#
return context.standard_masterlook.macros['standard']


#
# Suppose you want to improve getHeader()
# 
if somecookie == 'textmode':
    return context.standard_masterlook_text.macros['standard']
else:
    return context.standard_masterlook.macros['standard']

Explanation:
Basically, get used to reference once common single method for the macros when using "metal:use-macro..."
That's more scalable I think.


Comments:

good idea by wazum - 2004-10-14
great, so e.g. you can check the browser version first and decide which macro to use, but the document doesn't change this way ... you'll always use getHeader() ... ;-) youp!


A good example for DTML transitioners by tony - 2004-10-14
This is a really good example of how ZPT does things a lot better than DTML.

We're using essentially the unmodified code to switch between three or more
layouts.

The macro facility in ZPT is extremely useful - tends to make up a bit for the lack
of the <dtml-if> ... <dtml-else> ... </dtml-if> construct.
 
Re: Bah! by dudek - 2004-10-14
As a DTML die-hard, I don't see how this relates to
DTML vs. templates.  If the top include 
is a <dtml-var > call to the python script, then
it's basically the same story.


Syntax Question by sburch - 2004-10-14
The PT doc which houses my whole-page macro has
the id standard_template.html.  What would the syntax
be in the python script to reference the macro in this
file?

return contect.standard_template.html.macros['mainpage'] NOPE

shouldn't this work?

return context['standard_template.html'].macros['mainpage']
 
Re: Syntax Question by peterbe - 2004-10-14
Does the second example not work?

If so, try::
 return getattr(context, 'standard_template.html').macros['mainpage']