[Grok-dev] Re: a problem with 'static' and macros

Philipp von Weitershausen philipp at weitershausen.de
Wed May 23 06:10:38 EDT 2007


Martijn Faassen wrote:
> I have discovered a problem with the combination of the 'static' 
> variable in page templates in combination with macros.
> 
> 'static' is a special variable that Grok makes available to page 
> templates. It makes it easy to refer to static resources in the 
> package's 'static' directory:
> 
> <img tal:attributes="src static/myimage.jpg" />
> 
> Now put the above line in a macro. Now put the macro in one package, and 
> the page template that uses this macro in another package.
> 
> When the second page template is rendered, the 'static' in the macro 
> will reference the static directory in the *new* package, not the 
> package the macro itself is in. This is unexpected, as it disallows the 
> reuse of macros from other packages if they refer to static resources at 
> all.
> 
> We need a solution to this problem. I am at a loss; macros just work 
> this way with names, after all. Is it possible to have a "macro-local" 
> name only dependent on the view of the macro itself? I have no idea how. 
> Anyone got any ideas?

ZPT macros work as if the macro markup was pasted into the template 
that's using it. It's not like a function call that has its own scope.

That said, you can either use Kevin's suggestion and get to it via 
context/@@view_exposing_the_macro/static (basically, the macro would be 
looking itself up as a view), or the macro can refer to its resource 
directory explicitly by package name: tal:define="macro_static 
context/@@package.that.the.macro.is.defined.in"


-- 
http://worldcookery.com -- Professional Zope documentation and training


More information about the Grok-dev mailing list