Re: [Cheetahtemplate-discuss] Bug in NameMapper and global variables?
Brought to you by:
rtyler,
tavis_rudd
From: Mike O. <slu...@gm...> - 2006-05-03 21:10:30
|
On 5/3/06, Brian Bird <bri...@se...> wrote: > Perhaps I should really try and explain what I'm trying to do - there may= be > a better way: > I have a parent template with a function #def which requires the use of a > variable. There are two child templates which use the function in several > places, but the variable is different for each template. In python, it's > like this: > > class Parent: > def func(self): > print "Hello", self.variable > > class Child1(Parent): > def __init__(self): > self.variable =3D "Child1 variable" > def main(self): > self.func() > > class Child2(Parent): > def __init__(self): > self.variable =3D "Child2 variable" > def main(self): > self.func() > > class GrandChild1(Child1): > def main(self): > self.func() > Child1().main() ### prints Hello Child1 variable > Child2().main() ### prints Hello Child2 variable > GrandChild1().main() ### prints Hello Child1 variable because > self.variable is inherited from Child1 > > In Cheetah, I want each of the child/grandchild templates to be able to > define an object which can be used in #def's in the parent template. I > thought I had to use #set global for this. My Cheetah equivalent (I've > missed out Child2 for clarity) is: > > ----parent.tmpl---- > #def func() > Hello $globalVar > #end def > > ----child1.tmpl---- > #extends parent > #implements respond > #set global globalVar=3D"child1 variable" > $func() > > ----grandchild1.tmpl---- > #extends child1 > #implements respond > $func() Use #attr instead of '#set global'. This sets a class attribute in the child template, which can be read by the parent. I'm assuming the value is constant in each subclass; it doesn't change at runtime or per-instance. Another strategy, which may not be appropriate in this case but is good to remember, is to pass the value as a method argument. I've never used '#set global' and can't think of a case where it's necessary. Is anybody else using it? It's kind of confusing so maybe we should deprecate it. Anything in the searchList is global across subtemplates, so why not put the value there instead? Also, in Cheetah 2 there's '#set moduleglobal' which is simpler and more Pythonic than '#set global'. -- Mike Orr <slu...@gm...> (ms...@oz... address is semi-reliable) |