[ZPT] Picking a macro on the fly

Chris Meyers chris@hddesign.com
Tue, 19 Feb 2002 09:20:02 -0600


This worked except for one small issue. I got the following error:
Macro expansion failed
Products.PageTemplates.TALES.TALESError: exceptions.AttributeError on getMasterPy in "<PythonExpr here.getMasterPy(1).macros['master']>", at line 1, column 1

until I actually made the python script. After that everything worked perfectly.

Thanks a lot for the help,
Chris

On Tue, Feb 19, 2002 at 12:43:21PM +0100, Arnar Lundesgaard wrote:
> I might have misunderstood what you want to achieve, but here is
> an example of what I use. The script 'getMasterPy' returns a 
> 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=boolean
> if boolean:
>   pt=getattr(context,'master.html')
> else:
>   pt=getattr(context,'anothermaster.html')
> 
> return pt
> 
> <!-- begin master.html -->
> <html metal:define-macro="master">
>   <head>
>     <title tal:content="here/title">master.html</title>
>   <head>
>   <body>
>     <h1>Default Master</h1>
>     <metal:block define-slot="main" />
>   </body>
> </html>
> <!-- end master.html -->
> 
> <!-- begin anothermaster.html -->
> <html metal:define-macro="master">
>   <head>
>     <title tal:content="here/title">master.html</title>
>   <head>
>   <body>
>     <h1>Another Master</h1>
>     <metal:block define-slot="main" />
>   </body>
> </html>
> <!-- begin anothermaster.html -->
> 
> <!-- begin index.html -->
> <html metal:use-macro="python:here.getMasterPy(1).macros['master']">
>   <body>
>     <span metal:fill-slot="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="python:here.pickSomeMacroHere()">
> > ...
> > </html>
> > 
> > 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
> >
> >
> > -- 
> > Chris Meyers
> > 7941 Tree Lane Suite 200
> > Madison WI 53717
> 
> _______________________________________________
> ZPT mailing list
> ZPT@zope.org
> http://lists.zope.org/mailman/listinfo/zpt