Thread: [htmltmpl] print several vars in one loop
Brought to you by:
samtregar
From: Offer K. <off...@gm...> - 2004-09-23 14:57:35
|
Hi, Say I've for got the following parameter passed to my template: names_loop => $names, Where $names looks like: $names = [ {name => name1}, {name => name2}, {name => name3}, # ... 50 such names ]; What do I write in my template to print 3 names per line, for example? Is this even possible or do I have to re-arrange my data? I tried: <tmpl_loop names_loop><tmpl_var name> <tmpl_var name> <tmpl_var name> </tmpl_loop> But that just printed each name 3 times :-( Thanks in advance, -- Offer Kaye |
From: Roger B. W. <ro...@fi...> - 2004-09-23 15:31:23
|
On Thu, Sep 23, 2004 at 04:56:33PM +0200, Offer Kaye wrote: > >What do I write in my template to print 3 names per line, for example? What I tend to do is: In the data, put in a "linebreak" variable: $names = [ {name => name1}, {name => name2}, {name => name3, linebreak => 1}, etc. then use it in the template: <tmpl_loop names_loop><tmpl_var name> <tmpl_if name=linebreak><br></tmpl_if> </tmpl_loop> R |
From: Mark S. <ma...@su...> - 2004-09-23 15:48:45
|
On 2004-09-23, Offer Kaye <off...@gm...> wrote: > Hi, > Say I've for got the following parameter passed to my template: > names_loop => $names, > > Where $names looks like: > $names = > [ > {name => name1}, > {name => name2}, > {name => name3}, > # ... 50 such names > ]; > > What do I write in my template to print 3 names per line, for example? > Is this even possible or do I have to re-arrange my data? > I tried: > <tmpl_loop names_loop><tmpl_var name> <tmpl_var name> <tmpl_var name> > </tmpl_loop> > > But that just printed each name 3 times :-( So you want your data to look like this? Name1 Name2 Name3 Name4 Name5 Name6 One option is to create a nested loop and send that data structure to the code. It will look like this: $names = [ { row => [ {name => name1}, {name => name2}, {name => name3}, ], }, { row => [ {name => name3}, {name => name4}, {name => name5}, ], }, ]; ### Alternatively, you could try the HTML::Template::Expr module (which I have never used). It should allow you to express logic like: "After after third table cell, end the current row and start a new one". (Hint: use the modulo operator: "%"). Mark |
From: Jason P. <ja...@jo...> - 2004-09-23 15:50:36
|
What I've done in the past, where I need to have more than two in a row is to create a nested loop, where the outer loop is the rows and the inner loop is the 3 (or N != 2) records you want to display. Code Example (in the cgiapp): # Need to divide into sixths for Rosie's design... my $count = 0; my $six_issues = []; my $rows = []; while( my $hr = $sth->fetchrow_hashref() ) { $hr->{'bi_rate'} = sprintf( "%.2f", $hr->{'bi_rate'} ); $count++; my $img = 'graphics/TOC/' . sprintf( "%02d", $hr->{'volume_num'} ) . sprintf( "%02d", $hr->{'issue_num'} ) . '/cover-small.jpg'; push @$six_issues, { 'image_file' => $img, 'id' => $hr->{'id'}, 'label' => $hr->{'label'}, 'volume_num' => $hr->{'volume_num'}, 'issue_num' => $hr->{'issue_num'}, 'bi_rate' => $hr->{'bi_rate'}, }; if ( $count % 6 == 0 ) { push @$rows, { 'six_issues' => $six_issues }; $six_issues = []; } } push @$rows, { 'six_issues' => $six_issues } if ( $six_issues ); $template->param( 'rows' => $rows ); ---------------------- htmltmpl Code Example: ---------------------- <!-- TMPL_LOOP NAME="rows" --> <tr> <!-- TMPL_LOOP NAME="six_issues" --> <td valign="top" nowrap class="sansserif"><label for="ind_<!-- TMPL_VAR NAME="id" -->"><img src="<!-- TMPL_VAR NAME="image_file" -->" border="1" width="92" height="120"></label></td> <!-- /TMPL_LOOP --> </tr> <tr> <!-- TMPL_LOOP NAME="six_issues" --> <td valign="top" nowrap class="sansserif"><label for="ind_<!-- TMPL_VAR NAME="id" -->"><!-- TMPL_VAR NAME="label" --> <br> (Vol. <!-- TMPL_VAR NAME="volume_num" -->, Issue <!-- TMPL_VAR NAME="issue_num" -->)<br> <input type="checkbox" name="ind_issue" id="ind_<!-- TMPL_VAR NAME="id" -->" value="<!-- TMPL_VAR NAME="id" -->"> $ <!-- TMPL_VAR NAME="bi_rate" --> (USD)</label> </td> <!-- /TMPL_LOOP --> </tr> <!-- /TMPL_LOOP --> You can see this in action at our FB&C Subscription Website (it's the 2nd process {we don't collect your information from the first step -- it all becomes hidden variables at the bottom of that 2nd page}): http://www.finebooksmagazine.com/subscribe This is one of the limitations of HTML::Template, but it's also a bleed or merge of logic/control into the presentation layer. I (& you) might get tempted to move to something like TT, where you can put some logic in the template itself, but that's where you blur the lines of MVC. I would be interested to read other responses to this, too. Cheers, Jason PS: If you only wanted two, you could workaround it with the __EVEN__ and __ODD__ loop context variables. |