Discussion:
[htmltmpl] TMPL_LOOP limit or count
David A. Bandel
2011-04-29 21:28:01 UTC
Permalink
Folks,

Been using HTML::Template in a few projects. Really nice not to have
HTML tangled in the Perl code.

Got a largish hash I am sorting. Want to output only about 15 lines
of the table. Is there a way to tell the loop to stop?

Here's what I'm doing:

my @calloutdata;
foreach $callskey ( sort {$callnum{$b} <=> $callnum{$a}} keys %callnum){
my %outbound;
$outbound{EXTENNUMCALLED} = $callskey;
$outbound{TIMESCALLED} = $callnum{$callskey};
push( @calloutdata, \%outbound );
}

$template->param( OUTBOUNDS => \@calloutdata );

The calloutdata array winds up to be about 200 lines long. Any way to
have the TMPL_LOOP stop output after 15 or so table lines?

Nothing in the docs suggests a way to do this (or am I missing something?).

TIA,

David A. Bandel
--
Focus on the dream, not the competition.
            - Nemesis Air Racing Team motto
Visit my web page at: http://david.bandel.us/
Michael Peters
2011-04-29 21:37:13 UTC
Permalink
foreach $callskey ( sort {$callnum{$b}<=> $callnum{$a}} keys %callnum){
my %outbound;
$outbound{EXTENNUMCALLED} = $callskey;
$outbound{TIMESCALLED} = $callnum{$callskey};
}
The calloutdata array winds up to be about 200 lines long. Any way to
have the TMPL_LOOP stop output after 15 or so table lines?
Well, the first thing I'd do is suggest that if possible you just don't
put all 200 things into the loop. That would save on your processing if
you didn't have to create the big loop to begin with.

But if you needed them in the loop in some places but didn't want to
display them in others, you can use the __counter__ loop context var
combined with HTML::Template::Expr.

<tmpl_loop outbounds>
<tmpl_if expr="__counter__ <= 15">
...
</tmpl_if>
</tmpl_loop>
Nothing in the docs suggests a way to do this (or am I missing something?).
It's not possible with just HTML::Template, but HTML::Template::Expr
should help you get there.
--
Michael Peters
Plus Three, LP
David A. Bandel
2011-04-30 01:04:08 UTC
Permalink
foreach $callskey ( sort {$callnum{$b}<=>  $callnum{$a}} keys %callnum){
    my %outbound;
    $outbound{EXTENNUMCALLED}  = $callskey;
    $outbound{TIMESCALLED}      = $callnum{$callskey};
}
The calloutdata array winds up to be about 200 lines long.  Any way to
have the TMPL_LOOP stop output after 15 or so table lines?
Well, the first thing I'd do is suggest that if possible you just don't put
all 200 things into the loop. That would save on your processing if you
didn't have to create the big loop to begin with.
Unfortunately, in order to do what I'm doing (you haven't seen the
code that comes before), I need all the values all at once or the sort
on the value of the key/value pair doesn't work.
But if you needed them in the loop in some places but didn't want to display
them in others, you can use the __counter__ loop context var combined with
HTML::Template::Expr.
It's an idea. But hoping to keep it simple.
<tmpl_loop outbounds>
 <tmpl_if expr="__counter__ <= 15">
   ...
 </tmpl_if>
</tmpl_loop>
Nothing in the docs suggests a way to do this (or am I missing something?).
It's not possible with just HTML::Template, but HTML::Template::Expr should
help you get there.
--
Michael Peters
Plus Three, LP
Will keep this in mind.

Thanx,

David A. Bandel
--
Focus on the dream, not the competition.
            - Nemesis Air Racing Team motto
Visit my web page at: http://david.bandel.us/
v***@gmail.com
2011-04-29 21:45:26 UTC
Permalink
Just a different way of thinking about it, but you could always output all the rows and use html/css to hide the ones after #15 or whatever. That way you're a bit more in keeping with data/presentation split that HTML::Template enforces...

Vijay Krishna Ramesh
***@gmail.com

----- Reply message -----
From: "David A. Bandel" <***@gmail.com>
Date: Fri, Apr 29, 2011 17:28
Subject: [htmltmpl] TMPL_LOOP limit or count
To: <html-template-***@lists.sourceforge.net>

Folks,

Been using HTML::Template in a few projects. Really nice not to have
HTML tangled in the Perl code.

Got a largish hash I am sorting. Want to output only about 15 lines
of the table. Is there a way to tell the loop to stop?

Here's what I'm doing:

my @calloutdata;
foreach $callskey ( sort {$callnum{$b} <=> $callnum{$a}} keys %callnum){
my %outbound;
$outbound{EXTENNUMCALLED} = $callskey;
$outbound{TIMESCALLED} = $callnum{$callskey};
push( @calloutdata, \%outbound );
}

$template->param( OUTBOUNDS => \@calloutdata );

The calloutdata array winds up to be about 200 lines long. Any way to
have the TMPL_LOOP stop output after 15 or so table lines?

Nothing in the docs suggests a way to do this (or am I missing something?).

TIA,

David A. Bandel
--
Focus on the dream, not the competition.
            - Nemesis Air Racing Team motto
Visit my web page at: http://david.bandel.us/
David A. Bandel
2011-04-30 01:00:26 UTC
Permalink
unknown
1970-01-01 00:00:00 UTC
Permalink
Just a different way of thinking about it, but you could always output al=
l
the rows and use html/css to hide the ones after #15 or whatever. That wa=
y
you're a bit more in keeping with data/presentation split that
HTML::Template enforces...
Knew I was missing something. Michael suggested in another post
HTML::Template::Expr, but I like the CSS method a little better.

Thanx.
Vijay Krishna Ramesh
----- Reply message -----
Date: Fri, Apr 29, 2011 17:28
Subject: [htmltmpl] TMPL_LOOP limit or count
Folks,
Been using HTML::Template in a few projects. =A0Really nice not to have
HTML tangled in the Perl code.
Got a largish hash I am sorting. =A0Want to output only about 15 lines
of the table. =A0Is there a way to tell the loop to stop?
foreach $callskey ( sort {$callnum{$b} <=3D> $callnum{$a}} keys %callnum)=
{
=A0 =A0my %outbound;
=A0 =A0$outbound{EXTENNUMCALLED} =A0=3D $callskey;
=A0 =A0$outbound{TIMESCALLED} =A0 =A0 =A0=3D $callnum{$callskey};
}
The calloutdata array winds up to be about 200 lines long. =A0Any way to
have the TMPL_LOOP stop output after 15 or so table lines?
Nothing in the docs suggests a way to do this (or am I missing something?=
).
[snip]


David A. Bandel
--=20
Focus on the dream, not the competition.
=A0 =A0 =A0 =A0 =A0 =A0 - Nemesis Air Racing Team motto
Visit my web page at: http://david.bandel.us/
Loading...