Hi All,
I'm playing with HTML::Template, and I've run into something that should be easy, but I don't see how it can work.
The docs say to pass many elements for a loop, you do this:
$template->param( loopvar => [ { name=>"foo",value=>"baloney" }, { name=>"bar",value=>"tiple" }, { name=>"blah",value=>"mulroney" }, ], );
So, I'm using anonymous array and hash operators to make an anonymous array of hash references. (page 246, "The anonymous array composer", camel book).
That's great, but what if you don't know your content at programming time?
This comes up frequently. Maybe the explanation(s) should be added to the H::T docs (htdocs!)
Apologies if you already know some of the following.
A 2-d table (columns x rows) is a common, dareisay, the only representation of a database table. That is how you will also (in all likelihood) display it on your html page. So, imagine that each row is a hash where each key is the fieldname, and the value is the value of that field for that row. Now, if you string up a bunch of such hashes in a list, you get an array of hashes. Of course, since an array can store only scalara, it is actually an array of refs to hashes. And, since the $template->param is nothing but a hash itself, and since value of a key in a hash is also a scalar, a ref to our array of refs to hashes is applied.
So, you have
$template->param('result' => $arrayref);
where arrayref is a ref to our array of refs to hashes.
If you are using DBI (which is what you would typically, to grab the data from a table), things get extremely easy. The $sth->fetchall_arrayref({}) conveniently fetches a ref to an array of refs to hashes... cool.
so,
$template->param('result' => $sth->fetchall_arrayref({}));
works groovy.
You can construct this programmatically if you are reading sequentially from a file.
my @result; while (<FILE>) { my %row; .. .. do something to split the row and stuff it into %row .. .. push(@result, \%row); } $template->param('result' => [EMAIL PROTECTED]);
The main problem is if you don't know the names of the fields you are pulling from a table (such as you would if you were to SELECT * FROM table). Since the fieldnames become the hash keynames, you would not be able to display them in your html page, because you wouldn't know what you pulled. To get around this, you simply treat each row as an array of hashes where each hash contains only one key called 'field' (or whatever), and its value is the value of that field. So you end up with an array of hashes where each hash is an array of hashes where each hash is a single field and its value. Cool.
Please always use strict and -w.
Hth.
------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. Does SourceForge.net help you be more productive? Does it help you create better code? SHARE THE LOVE, and help us help YOU! Click Here: http://sourceforge.net/donate/ _______________________________________________ Html-template-users mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/html-template-users