[htmltmpl] [patch] __counter__ variable
Brought to you by:
samtregar
From: simran <sim...@le...> - 2002-07-11 23:49:48
|
i All, I sent the patch below (for a __COUNTER__) variable if loop_context_vars was on - a while ago, but did not hear anything about it... maybe people didn't find it that useful (or maybe it didn't make it to the list). Anyhow, Sam, what do you think about the patch below? It would be useful with pringing lists where you want them numbered, and also for things like: <tmpl_expr name="__COUNTER__ % 7"> <tr> </tmpl_expr> for Table tr's if using a the HTML::Template::Expr template... simran. On Thu, 2002-04-18 at 09:40, simran wrote: > Hi All, > > There has recently been brief mention/discussion of the need for a > 'counter' variable in a loop. > > Finding such a feature invaluable myself, i have included a patch which > makes a '__COUNTER__' variable available if loop_context_vars is turned > on (i had version 2.4 of HTML::Template). > > Hope it makes it into the core... sam? > > kind regards, > > simran. > > *** Template.pm.orig Thu Apr 18 09:25:07 2002 > --- Template.pm Thu Apr 18 09:30:56 2002 > *************** > *** 617,629 **** > default) four loop context variables are made available inside a loop: > __FIRST__, __LAST__, __INNER__, __ODD__. They can be used with > <TMPL_IF>, <TMPL_UNLESS> and <TMPL_ELSE> to control how a loop is > ! output. Example: > > <TMPL_LOOP NAME="FOO"> > <TMPL_IF NAME="__FIRST__"> > This only outputs on the first pass. > </TMPL_IF> > > <TMPL_IF NAME="__ODD__"> > This outputs every other pass, on the odd passes. > </TMPL_IF> > --- 617,636 ---- > default) four loop context variables are made available inside a loop: > __FIRST__, __LAST__, __INNER__, __ODD__. They can be used with > <TMPL_IF>, <TMPL_UNLESS> and <TMPL_ELSE> to control how a loop is > ! output. > ! > ! In addition to the above, a __COUNTER__ VAR is also made available > ! when loop context variables are turned on. > ! > ! Examples: > > <TMPL_LOOP NAME="FOO"> > <TMPL_IF NAME="__FIRST__"> > This only outputs on the first pass. > </TMPL_IF> > > + <TMPL_VAR NAME="__COUNTER__"> > + > <TMPL_IF NAME="__ODD__"> > This outputs every other pass, on the odd passes. > </TMPL_IF> > *************** > *** 1860,1869 **** > # die_on_bad_params set output() will might cause errors > # when it tries to set them. > if ($options->{loop_context_vars}) { > ! $pmap{__first__} = HTML::Template::VAR->new(); > ! $pmap{__inner__} = HTML::Template::VAR->new(); > ! $pmap{__last__} = HTML::Template::VAR->new(); > ! $pmap{__odd__} = HTML::Template::VAR->new(); > } > } elsif ($which eq '/TMPL_LOOP') { > --- 1867,1877 ---- > # die_on_bad_params set output() will might cause errors > # when it tries to set them. > if ($options->{loop_context_vars}) { > ! $pmap{__first__} = HTML::Template::VAR->new(); > ! $pmap{__inner__} = HTML::Template::VAR->new(); > ! $pmap{__last__} = HTML::Template::VAR->new(); > ! $pmap{__odd__} = HTML::Template::VAR->new(); > ! $pmap{__counter__} = HTML::Template::VAR->new(); > } > > } elsif ($which eq '/TMPL_LOOP') { > *************** > *** 2731,2741 **** > @{$value_set}{qw(__first__ __inner__ __last__)} = (0,1,0); > } > $odd = $value_set->{__odd__} = not $odd; > } > $template->param($value_set); > $result .= $template->output; > $template->clear_params; > ! @{$value_set}{qw(__first__ __last__ __inner__ __odd__)} = > (0,0,0,0) > if ($loop_context_vars); > $count++; > } > --- 2739,2750 ---- > @{$value_set}{qw(__first__ __inner__ __last__)} = (0,1,0); > } > $odd = $value_set->{__odd__} = not $odd; > + $value_set->{__counter__} = $count + 1; > } > $template->param($value_set); > $result .= $template->output; > $template->clear_params; > ! @{$value_set}{qw(__first__ __last__ __inner__ __odd__ > __counter__)} = (0,0,0,0,0) > if ($loop_context_vars); > $count++; > } |