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

I am using TMPL_LOOP that works just fine. However when no rows are from the Database I get the following error:

HTML::Template::param() : attempt to set parameter 'logged_events' with a scalar - parameter is not a TMPL_VAR!

relevant code :
my $sth_le = $dbh->prepare($loggedEventsSQL); $sth_le->execute(); my $logged_events ; push @{$logged_events} , $_ while $_ = $sth_le->fetchrow_hashref(); my $cordr_template = HTML::Template->new(filename=>$pdp_template, die_on_bad_params =>0, ); $cordr_template->param( KPI => $kpi, THRES => $pdp_thres, DATETIME => $currDateTime, NEW_EVENTS => $new_events, LOGGED_EVENTS => $logged_events, );
  • Comment on HTML::Template - how to handle TMPL_LOOP parameter if no rows returned
  • Download Code

Replies are listed 'Best First'.
Re: HTML::Template - how to handle TMPL_LOOP parameter if no rows returned
by moritz (Cardinal) on Mar 04, 2011 at 09:50 UTC
    my $logged_events = []; should do the trick.

    But it reads much nicer if you do it this way:

    my @logged_events; push @logged_events, $_ while $_ = $sth_le->fetchrow_hashref(); my $cordr_template = HTML::Template->new(filename=>$pdp_template, die_on_bad_params =>0, ); $cordr_template->param( KPI => $kpi, THRES => $pdp_thres, DATETIME => $currDateTime, NEW_EVENTS => $new_events, LOGGED_EVENTS => \@logged_events, ) +;

    (modulo some code alignment you could also improve)

      Thanks - It now works as expected !!!

      Also thank you for additional advice
Re: HTML::Template - how to handle TMPL_LOOP parameter if no rows returned
by scorpio17 (Canon) on Mar 04, 2011 at 18:56 UTC

    If you need to handle the special case of having no data, you can do something like this in your template file:

    <TMPL_IF LOGGED_EVENTS> <TMPL_LOOP LOGGED_EVENTS> ... </TMPL_LOOP> <TMPL_ELSE> No Data! </TMPL_IF>
Re: HTML::Template - how to handle TMPL_LOOP parameter if no rows returned
by jfraire (Beadle) on Mar 05, 2011 at 06:44 UTC

    You are not doing any bindings or anything in the while loop. Instead of preparing, executing and then fetching one row at a time, your code would be simpler if you used:

    my $logged_events = $dbh->selectall_arrayref($loggedEventsSQL);

    By doing this you would also get an empty arrayref in case no rows are returned, thus avoiding the error from HTML::Template.

    Regards,

    Julio