Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

(dkubb) Re: (2) HTML::Template - Two columns?

by dkubb (Deacon)
on Jul 04, 2001 at 06:05 UTC ( #93752=note: print w/replies, xml ) Need Help??

in reply to HTML::Template - Two columns?

What you want to do is possible, but requires you to use an underused feature in HTML::Template called loop_context_vars. Enabling it in the new() constructor will allow you to use the loop context variables: __FIRST__, __LAST__, __INNER__, __ODD__ inside your TMPL_IF or TMPL_UNLESS tags. To see what I mean, check out the template example at the bottom of this post.

In particular, we care about the __ODD__ variable, which will be set to true on the odd numbered passes. By testing this variable, we can print either the <tr> or </tr> tags when appropriate. Consider the following code:

#!/usr/bin/perl -w use strict; use HTML::Template; my @scores = ( { name => 'name1', score => 4 }, { name => 'name2', score => 2 }, { name => 'name3', score => 3 }, ); my $template = HTML::Template->new( filename => 'scores.tmpl', loop_context_vars => 1, ); $template->param(scores => \@scores); print $template->output;

Which loads a template that contains the following text:

<table> <TMPL_LOOP NAME='scores'> <TMPL_IF NAME='__ODD__'> <tr> </TMPL_IF> <td><TMPL_VAR NAME='name'></td><td><TMPL_VAR NAME='score'></td> <TMPL_UNLESS NAME='__ODD__'> </tr> </TMPL_UNLESS> </TMPL_LOOP> </table>

This will iterate over the @scores and print them in two columns in the table. There is only one drawback to this approach, if the @scores has an odd number of elements, the final </tr> will not be printed. This can either be solved by pushing an empty element onto the array, or creative use of the __LAST__ and __ODD__ variables together. I personally prefer the latter and try to offload presentation logic off to the template, rather than pollute the code with special exceptions.

Replies are listed 'Best First'.
Re: (dkubb) Re: (2) HTML::Template - Two columns?
by mikeB (Friar) on Jul 05, 2001 at 18:48 UTC
    Thanks! That's perfect. Well, almost. Perfect would include generalization to more than just two columns ;)

    When I first read your message, I kicked myself for not RTFM carefully. Then went back to the FM and couldn't find a reference to __ODD__. How odd! A quick search on CPAN brought enlightenment: { CPAN => 2.3, PPM => 1.8 } I'm going to have to start relying only on CPAN rather than using Active State for its convenience on windows.

    Off to search for ways to load pod docs into the pretty html format...


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://93752]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2022-12-03 21:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found