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

I have a script which uses HTML::Template to throw different pages depending on the information received:

# open the HTML template my $template = HTML::Template->new(filename => $f_name); # fill in some parameters in the template $template->param(Number => $Number); $template->param(new => $new); $template->param(story => $story); $template->param(id_no => $id_no); $template->param(message_no => $message_no); if (($check == '0') or ($check == '3') or ($check == '4')) { $template->param(tart => $tart); $template->param(ear => $ear); $template->param(mouth => $mouth); } # print the template to file print $template->output;

My problem is that one of the pages does not need/contain the HTML line : <TMPL_VAR NAME="mouth">
The result is that when the template is called for that page a blank screen results

My workaround is simply to put that line into the template as a comment, and that works fine, but I thought I'd ask the question to see if there is a way of preventing the problem instead of stuffing the templates full of superfluous comments.

Thank you

PS I tried the following to no avail:

if (($check == '0') or ($check == '3') or ($check == '4')) { $template->param(tart => $tart); $template->param(ear => $ear); } if (($check == '0') or ($check == '4')) { $template->param(mouth => $mouth); }

Replies are listed 'Best First'.
Re: Unused variable in HTML::Template results in blank screen
by Abstraction (Friar) on Aug 09, 2003 at 05:08 UTC
    Give this a try:
    my $template = HTML::Template->new(filename => $f_name, die_on_bad_par +ams => 0);


    From the HTML::Template docs:

    die_on_bad_params - if set to 0 the module will let you call $template->param(param_name => 'value') even if 'param_name' doesn't exist in the template body. Defaults to 1.

      That worked very well - thanks for pointing it out. I was able to remove the conditional altogether so I think it will be a permanent piece of my armoury

      (BTW sorry about the RTFM aspect and thanks for not pointing it out - I relied on the info in the website as I still find module docs a bit beyond my ability. That should improve with experience, I hope!)

Re: Unused variable in HTML::Template results in blank screen
by antirice (Priest) on Aug 09, 2003 at 06:39 UTC

    Ummm...

    #!/usr/bin/perl -w use HTML::Template; my $temp = q|<TMPL_VAR name="mouth"><nothing-\|-bob><TMPL_VAR name="bo +b">|; $template = HTML::Template->new(scalarref => \$temp); $template->param(bob=>"bob"); print $template->output; __DATA__ <nothing-|-bob>bob

    You don't actually need to set the parameter as it will be regarded as '' if it's undefined. Try seeing how the parse stack is actually breaking up your code. To do this, try the following:

    #!/usr/bin/perl -w use HTML::Template; use Data::Dumper; my $template = HTML::Template->new(filename => $f_name,debug=>1); print Dumper($template);

    This should provide you with a play-by-play parsing of the template. Furthermore, if you don't want something displayed, why use comments? Why not just use TMPL_COND to check whether or not the variable is set? Example:

    #!/usr/bin/perl -w use HTML::Template; my $temp = q|<TMPL_IF bob>something and <TMPL_VAR bob> <TMPL_ELSE>bob failed</TMPL_IF> <TMPL_IF joe>not displayed<TMPL_ELSE>joe failed</TMPL_IF> <TMPL_IF jim>not displayed either because 0 is false <TMPL_ELSE>jim failed</TMPL_IF>|; my $template = HTML::Template->new(scalarref=>\$temp); $template->param(bob=>"bob",jim=>0); print $template->output; __DATA__ something and bob joe failed jim failed

    Finally, what HTML is the template producing? Any unclosed comments that may have just been a copy and paste error? This has happened to me before as well. Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Unused variable in HTML::Template results in blank screen
by jeffa (Bishop) on Aug 09, 2003 at 13:32 UTC
    Abstraction solved the symptom, antirice showed how to find the problem ... i just want to show you how to indent your code:
    # fill in some parameters in the template $template->param(Number => $Number); $template->param(new => $new); $template->param(story => $story); $template->param(id_no => $id_no); $template->param(message_no => $message_no); if (($check == '0') or ($check == '3') or ($check == '4')) { $template->param(tart => $tart); $template->param(ear => $ear); $template->param(mouth => $mouth); }
    There is no excuse for not finding a style of code indentation and sticking to it. No excuse, especially when you present that code to others for review. It's really hard to tell which lines belong in a block if you don't use indentation. And please don't use the excuse everyone gives: "Well, i was going to go back and put indentation in later ..." ... do it now.

    And if you don't know how to indent your code properly, then by all means let Perltidy decide for you. I would rather all newcomers use Perltidy to indent their code than have them just slam stuff to us. Oh and by the way ... HTML::Template::param() accepts a list, so you can say stuff like:

    # fill in some parameters in the template $template->param( Number => $Number, new => $new, story => $story, id_no => $id_no, message_no => $message_no, );
    Hope this helps. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      No excuses - I accept the smacked botty. I do indent generally, and am not sure why I didn't in this case. All part of the learning curve, I suppose.

      I'm very interested in what you say about param() accepting a list as this point doesn't come out in Sam Tregar's explanation he uses the code as I do (tho' with impeccable indentation):

      # fill in some parameters in the template $template->param(home => $ENV{HOME}); $template->param(path => $ENV{PATH});
      So thanks very much for that.