Re: [Cheetahtemplate-discuss] Fwd: $self?
- On 10/26/06, Mike Meyer <mwm@...> wrote:
> In <6e9196d20610261359r115a0da8x5e210e957a92a907@...>, Mike Orr <sluggoster@...> typed:If you really want a template class attribute, it's:
> > On 10/25/06, Tavis Rudd <tavis@...> wrote:
> > > Case 2: I needed class variables. Not just attributes, but real live
> > > variables; basically I needed a value that changed for each instance
> > > of the class. I forget exactly what I tried, but I wound up doing:
> > >
> > > #set $self.instance_counter += 1
> > >
> > > to get things to change properly.
> > Not "class variables"; this is a template instance attribute.
> I think I may have left out an important part:
> #attr $instance_counter = 
> I.e. - instance_counter is an attribute of the class the template
> compiles to, not of an instance of that class. Or maybe that's what
> "template instance attribute" means.
> > The value will leak from one template fill to the next, so you'll have
> > to intialize it to 0 at the beginning of every fill. Also, modifying
> > template attributes is thread unsafe.
> And here's why I said I wanted "class variables" - I *want* the value
> to change between fills.
#set $self.__class__.instance_counter = 0
#set $self.__class__.instance_counter += 1
I almost put that in my example but thought, "That can't be what he
wants. Nobody in their right mind would share a variable across
template instances." But it looks like you found a use for it.
A template instance is what Template(...) returns. A template class
is what Template.compile(...) returns -- it has to be instantiated
before being filled. A template class is also what a compiled
template module contains.
'set global' variables won't be shared across fills. But class
attributes won't be shared with #include files. If it has to be
global across everything, put it in a Python module and #import the
Mike Orr <sluggoster@...>
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
Cheetahtemplate-discuss mailing list