dejoha has asked for the wisdom of the Perl Monks concerning the following question:

I just began using HTML::Template and am trying to create an HTML table. In all the examples I've read so far (POD, online), the tables are very basic -- one set of data per row and column, vis:

+--------+--------+
| Name   | Phone  |
+--------+--------+
| Name2  | Phone2 |
+--------+--------+
...

I would like to do the following:

+-------+-------+-------+-------+
| Name  | Name1 | Name2 | Name3 |
| Phone | Phone1| Phone2| Phone3|
+-------+-------+-------+-------+
| Name4 | Name5 | Name6 | Name7 |
| Phone4| Phone5| Phone6| Phone7|
+-------+-------+-------+-------+

Does this make sense?

My code does not work:

<table border="0"> <TMPL_LOOP NAME="PHONE_LOOP"> <tr> <td> <TMPL_VAR NAME="NAME"> <br> <TMPL_VAR NAME="PHONE"> </td> <td> <TMPL_VAR NAME="NAME"> <br> <TMPL_VAR NAME="PHONE"> </td><td> <TMPL_VAR NAME="NAME"> <br> <TMPL_VAR NAME="PHONE"> </td><td> <TMPL_VAR NAME="NAME"> <br> <TMPL_VAR NAME="PHONE"> </td> </tr> </TMPL_LOOP> </table>

This, of course, just repeats the same pair of variables four times in the row, instead of getting the first four sets. I know I could create the rows in the Perl script, but the idea of the HTML::Template would be that I could tweak the template to adjust how many columns.

Any help, pointers, docs would be very helpful. Thanks

Replies are listed 'Best First'.
Re: Column Count w/ HTML::Template
by PodMaster (Abbot) on Nov 02, 2003 at 08:18 UTC
    This, of course, just repeats the same pair of variables four times in the row, instead of getting the first four sets.
    That's because you only have two variables, NAME and PHONE (it's like writing print "$name $phone"; 5 times, $name and $phone are still $name and $phone). I suggest you checkout the tutorial in Tutorials and/or the one on the official website, which is at http://html-template.sourceforge.net/.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Column Count w/ HTML::Template
by BUU (Prior) on Nov 02, 2003 at 10:12 UTC
    <table> <tmpl_loop foo> <tr> <tmpl_loop bar> <td> <tmpl_var one> <tmpl_var two> </td> </tmpl_loop> </tr> </tmpl_loop> </table> <code> <br> <code> $ht=new HTML::Template(); my @data= ( { bar => [one => 'stuff', two => 'otherstuff'] }, { bar => [one => 'stuff2', two => 'otherstuff2'] } ); $ht->params(foo=>[@data]); print $ht->output;
    In other words, nest tmpl_loops.
      use strict; use warnings; use HTML::Template; my $template = HTML::Template->new(filehandle => \*DATA); $template->param(foo => [ { bar => [{one => 'stuff', two => 'otherstuff'}] }, { bar => [{one => 'stuff2', two => 'otherstuff2'}] } ]); print $template->output; __DATA__ <table> <tmpl_loop foo> <tr> <tmpl_loop bar> <td> <tmpl_var one><br/> <tmpl_var two> </td> </tmpl_loop> </tr> </tmpl_loop> </table>
      In other words, please post complete examples (no assembly required) and make sure your code works first (you left out some important curly braces that enable your code to compile).

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Column Count w/ HTML::Template
by atcroft (Abbot) on Nov 02, 2003 at 11:04 UTC

    You may also want to check out the responses to my posting of a similar question.

Re: Column Count w/ HTML::Template
by cees (Curate) on Nov 02, 2003 at 16:13 UTC

    You can do this very easily with HTML::Template::Expr. Just activate the loop_context_vars options so that you have access to the loop counter __counter__. Then use the expression <TMPL_UNLESS __counter__ % 4> to mark your table boundaries.

    use HTML::Template::Expr; my $t = HTML::Template::Expr->new(filehandle => *DATA, loop_context_vars => 1); $t->param(data => [ map { {value => $_} } ('a'..'z') ] ); print $t->output; __DATA__ <table> <tr><TMPL_LOOP NAME="DATA"><td> <TMPL_VAR NAME="VALUE"> </td><TMPL_UN +LESS EXPR="(__counter__ % 4) || (__last__)"> </tr> <tr></TMPL_UNLESS></TMPL_LOOP></tr> </table>

    This will result in the following table:

    <table> <tr><td> a </td><td> b </td><td> c </td><td> d </td> </tr> <tr><td> e </td><td> f </td><td> g </td><td> h </td> </tr> <tr><td> i </td><td> j </td><td> k </td><td> l </td> </tr> <tr><td> m </td><td> n </td><td> o </td><td> p </td> </tr> <tr><td> q </td><td> r </td><td> s </td><td> t </td> </tr> <tr><td> u </td><td> v </td><td> w </td><td> x </td> </tr> <tr><td> y </td><td> z </td></tr> </table>

    If the designer wishes to change the number of columns, just change the 4 to the desired number...

    This kind of goes against the philosophy of HTML::Template by making the tempate look more complex than desired. However, I think this is the only way to allow the template to decide how to split up the columns. The other options mentioned leave that up to the programmer which is not always what you want. I think the number of columns to display is a design decision, not a programming decision.

    By the way, if you search the HTML::Template archives you will find many answers to this question...

    - Cees

Re: Column Count w/ HTML::Template
by Cody Pendant (Prior) on Nov 02, 2003 at 21:43 UTC
    I could tweak the template to adjust how many columns.

    Not sure what you mean by that -- what's your code supposed to do when the number of items isn't divisible by four?



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print