Hello all,
Been looking for a while for this answer, and seem to think it must have been asked before, although both the archives mentioned in the docs are down. So here goes:
Is there an easy way to pass a Hash to HTML::Template in hopes to use the key/value pairs, without putting it in a <TMPL_LOOP>?
Along with HTML::Template, I'm also familiar with Template-Toolkit. It had the dot syntax, where you can navigate a complex data structure like -
$this->{that}->{then}->{some}
like this:
[% this.that.then.some %]
I can't find anything similar in HTML::Template, except to put a <TMPL_LOOP> around the block of HTML when I want to access a value in a hash. I guess this is fine with me - as long as performance isn't completely dismal;
You could walk your way down the data structure and flatten it out using the dot notation. Here is a quick example of how you could do that. Note that it is not heavily tested, and doesn't take into consideration tied variables, code refs, objects, etc... Also, since it is recursive, it probably isn't that efficient!
sub flatten {
my $item = shift;
my $keypart = shift || '';
my $data = shift || {};
if (ref $item eq 'HASH') {
while (my($k,$v) = each %$item) {
$data = flatten($v, $keypart.'.'.$k, $data);
}
} elsif (ref $item eq 'ARRAY') {
for (my $i=0; $i < @$item; $i++) {
$data = flatten($item->[$i], $keypart.'.'.$i, $data);
}
} else {
$data->{substr($keypart,1)} = $item;
}
return $data;
}my $struct = {
test1 => 'value1',
test2 => [qw(a b c d e f)],
test3 => { test3a => [qw(g h i)], test3b => 'value3b' },
test4 => 'value4',
};my $data = flatten($struct);
And if you dump the resulting hash out, you get the following:
test1 = value1 test2.0 = a test2.1 = b test2.2 = c test2.3 = d test2.4 = e test2.5 = f test3.test3a.0 = g test3.test3a.1 = h test3.test3a.2 = i test3.test3b = value3b test4 = value4
Of course, if you really need this functionality, why not just use Template Toolkit since you already know it? Use the best tool for the job... Sometimes that will be HTML::Template, sometimes it may be something else.
Cheers,
Cees
To clarify, in my Perl code, I have a hash that holds any errors that may have been put forth from an HTML form. Something like:
my %errors = (forgot_name => 1, impossible_zipcode => 1, is_the_unabomber => 0);
my theoretically HTML would be something like:
<TMPL_IF EXPR="errors.forgot_name == 1">
<p>You forgot your name!</p>
</TMPL_IF>
Thanks for your time!
justinSimoni.Artist -- .: art http://justinsimoni.com .: phone 720 436 7701
------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ Html-template-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/html-template-users
------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ Html-template-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/html-template-users
