Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

IF in a loop using HTML::Template

by Anonymous Monk
on Mar 20, 2013 at 17:33 UTC ( #1024577=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
Using CGI::Application and I am trying to have an IF inside of the "tml_loop" and its giving me this error:
There has been an error: HTML::Template : Attempt to set nonexistent +parameter 'test' - this parameter name doesn't match any declarations + in the template file : (die_on_bad_params => 1)
I am trying to only show the buttons option if this sub named my_calls sets the value of "test" to true or "1". I dont understand why I am getting the error since I have the <TMPL_IF NAME="test"> tag in the .tmpl file. Could it be that the data coming from the database to populate the "data" tag loop is not yet ready?
The sample part of the Perl file
# temp.pl ... sub my_calls { my $self = shift; my $q = $self->query; my $name = $q->param('name'); my $sql = "select * from my_taable where name = ?"; my $alldata = $self->_get_data($sql, $name); my $template = $self->load_tmpl('mytemp.tmpl'); $template->param('data', $alldata ); $template->param('test' => 1); return $template->output(); } ...
And the portion of the .tmpl file
# mytemp.tmpl ... <form action="" method="post"> <tmpl_loop name=data> <tr> <td align="center"> <input type="checkbox" class="checkbox" name="confirm" value +="n" <tmpl_var name=checked>>&nbsp; </td> <td align="center"> <span id="id_<tmpl_var name=order>"> <tmpl_var name=number> </ +span> </td> <TMPL_IF NAME="test"> <table border="1" width="170" cellspacing="0" cellpadding="0"> <tr> <td> <input name="btnSave_<tmpl_var name=id>" type="button" + value="Save" id="btnSave_<tmpl_var name=id>""> <input name="btnEdit_<tmpl_var name=id>" type="button" + value="Edit" id="btnEdit_<tmpl_var name=id>""> <input name="btnDelete_<tmpl_var name=id>" type="button" + value="Delete" id="btnDelete_<tmpl_var name=id>""> </td> </tr> </table> </TMPL_IF> </td> </tr> </tmpl_loop> </form> ...

Comment on IF in a loop using HTML::Template
Select or Download Code
Re: IF in a loop using HTML::Template
by Anonymous Monk on Mar 20, 2013 at 19:13 UTC

    I dont understand why I am getting the error since I have the TMPL_IF NAME="test"> tag in the .tmpl file.

    Maybe you're loading the wrong template, who knows, so turn on debugging, and figure it out

    #!/usr/bin/perl -- use strict; use warnings; use HTML::Template; my $template = HTML::Template->new( die_on_bad_params => 1, #~ scalarref => \'<tmpl_if spectacles>glasses<tmpl_else>stars</tmp +l_if> ', scalarref => \'<tmpl_if name="spectacles">glasses<tmpl_else>stars< +/tmpl_if> ', debug => 1, #~ stack_debug => 1, ); $template->param( spectacles => 0 ); print $template->output; $template->param( spectacles => 1 ); print $template->output; print "\n"; __END__ ### HTML::Template Debug ### In _parse: ### HTML::Template Debug ### /fake/path/for/non/file/template : line 1 + : TMPL_IF spectacles start ### HTML::Template Debug ### /fake/path/for/non/file/template : line 1 + : ELSE ### HTML::Template Debug ### /fake/path/for/non/file/template : line 1 + : /TMPL_IF end ### HTML::Template Debug ### In output ### HTML::Template Debug ### In output stars glasses

    See Basic debugging checklist, brian's Guide to Solving Any Perl Problem

      I don't understand your point, definitely not loading the wrong template, using CGI::Application for this as well. You can see on the code that "test" has been declared in the .tmpl and called in the perl file. The template gets called as:
      my $template = $self->load_tmpl('mytemp.tmpl');

        I don't understand your point,

        Turn on debugging , and figure out where the problem is. The code you posted is not complete, I can't run it and figure it out for you.

Re: IF in a loop using HTML::Template
by kielstirling (Scribe) on Mar 20, 2013 at 19:58 UTC
    In your template when you look for the param 'test' you are in the data loop.
    tmpl_loops take a arrayref of hashref
    ie,
    $tmpl->param(data=>[{test=>1, foo=>1, bar=>1}]);
    My understanding is that you can't refer to params outside of the loop. Like you are doing with the test param.
    -Kiel
      Is there any documentation on this issue somewhere?
        From the POD ...

        Inside a <TMPL_LOOP>, the only variables that are usable are the ones from the <TMPL_LOOP>. The variables in the outer blocks are not visible within a template loop. For the computer-science geeks among you, a <TMPL_LOOP> introduces a new scope much like a perl subroutine call. If you want your variables to be global you can use global_vars option to new() described below.

        HTML::Template
        -Kiel

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024577]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-09-01 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls