HTML-Template - Array of Hash

663 Views Asked by At

I have two Array of Hashes: The first contains values for a current time interval and the second contains values for a previous time interval.

 @AoHcurrent=
( { node => "ABC", 
    link => "DEF", 
    time => "10:00", 
    value => "100", 
  }, 
  { 
    node => "FGH", 
    link => "IJK", 
    time => "10:00", 
    value => "200", 
   }, 
); 

@AoHprevious= 
( { node => "ABC", 
    link => "DEF", 
    time => "09:45",  
    value => "10", 
   }, 
   { node => "FGH", 
      link => "IJK", 
      time => "09:45", 
      value => "50", 
   }, 
);

I want to now use HTML-Template to present this data. Something like :

NODE LINK VALUE 
--------------------- 
ABC DEF 100(10) 
FGH IJK 200 (50)

the values in brackets represent the previous value.

my %html_template_parameters = 
 ( AOHCURRENT => \@AoHcurrent, 
   AOHPREVIOUS => \@AoHprevious, ); 

my $html_template=qq{Report.tmpl}; 
my $html_output=qq{Report.html}; 

htmlReport($html_template,$html_output,\%html_template_parameters);

where htmlReport is a function that generates the report

I require guidance on defining the Report.tmpl file.

Thanks you in advance

2

There are 2 best solutions below

0
On

You can't do that with 2 separate lists just with HTML::Template. And trying to do it with HTML::Template::Expr would be a nightmare to maintain. Try collapsing them into a single list where the hash data is merged.

0
On

see also http://www.perlmonks.org/?node_id=972954

I gave an example there how this can be solved with HTML::Template::Compiled.

Basically you would navigate through the parameter stash like this:

[%= expr=".AOHPREVIOUS[__index__]{'value'}" %]

or with the classic syntax:

<TMPL_VAR expr=".AOHPREVIOUS[__index__]{'value'}" >