[Petal] Caching of method call results.

Thedi Gerber gerber at id.ethz.ch
Mon Mar 2 08:54:42 GMT 2009


Hello,

I wrote a method which returns alternating 'even' and 'odd' for table rows:

sub row_class {
     my $self = shift;
     $self->{ row_class } = ! $self->{ row_class };
     return $self->{ row_class } ? 'even' : 'odd';
}

Actually the details are much more complicated, but the problem can be described with this example.

In a template I call this method

<tr class="odd"
     tal:attributes="class utils/row_class" >
...
<tr class="odd"
     tal:attributes="class utils/row_class" >

It seems that Petal caches method call results, hence I always get the same result from method 
row_class.


As a work around I tried to pass a dummy argument:

<tr class="odd"
     tal:attributes="class utils/row_class '1'" >
...
<tr class="odd"
     tal:attributes="class utils/row_class '2'" >

This works as desired, but is rather ugly.

Is there a better way?

Regards

Thedi gerber at id.ethz.ch



More information about the Petal mailing list