[ZPT] Picking a macro on the fly

Arnar Lundesgaard arnar.lundesgaard@creuna.no
Tue, 19 Feb 2002 12:43:21 +0100


I might have misunderstood what you want to achieve, but here is
an example of what I use. The script 'getMasterPy' returns a=20
different PageTemplate based on the inputvalue (1 or 0).
The TALES statement can of course be made prettier, but you
get the basic idea of how to extend it.

  - Arnar

------------------------------------
## Script (Python) getMasterPy
##parameters=3Dboolean
if boolean:
  pt=3Dgetattr(context,'master.html')
else:
  pt=3Dgetattr(context,'anothermaster.html')

return pt

<!-- begin master.html -->
<html metal:define-macro=3D"master">
  <head>
    <title tal:content=3D"here/title">master.html</title>
  <head>
  <body>
    <h1>Default Master</h1>
    <metal:block define-slot=3D"main" />
  </body>
</html>
<!-- end master.html -->

<!-- begin anothermaster.html -->
<html metal:define-macro=3D"master">
  <head>
    <title tal:content=3D"here/title">master.html</title>
  <head>
  <body>
    <h1>Another Master</h1>
    <metal:block define-slot=3D"main" />
  </body>
</html>
<!-- begin anothermaster.html -->

<!-- begin index.html -->
<html metal:use-macro=3D"python:here.getMasterPy(1).macros['master']">
  <body>
    <span metal:fill-slot=3D"main">
      <p>content</p>
    </span>
  </body>
</html>
<!-- end index.html -->

On 18. februar 2002 22:47, Chris Meyers wrote
> Hey all,
> Is there a way to mix tal and metal in the same tag so that
> you can pick a macro on the fly? What I would like to do is
> something like this:
>
> <html metal:use-macro=3D"python:here.pickSomeMacroHere()">
> ...
> </html>
>=20
> where pickSomeMacroHere() is a python script which selects a
> macro based on the user. If you can't mix tal and metal, is there
> any other way to do this. I've looked through the ZPT tutorials
> and the mailing list, but I haven't found any info regarding this.
>
> Any suggestions appreciated,
> Chris
>
>
> --=20
> Chris Meyers
> 7941 Tree Lane Suite 200
> Madison WI 53717