Mr.T has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone! I'm still new here, but I am working on a small project in Perl that will *hopefully* use the module HTML::Template. I need to find out how to exactly use it though, and I can't find any simple documentation or instructions anywhere. Could anyone break it down for me? Thanks!!

Mr.T
qw/"I pity da foo' who don't use Perl!"/;

Replies are listed 'Best First'.
Re: HTML::Template Help
by blakem (Monsignor) on Aug 09, 2001 at 10:57 UTC
    Have you seen the beginers article over on perlmonth? It should get you up to speed prety quickly.

    -Blake

Re: HTML::Template Help
by busunsl (Vicar) on Aug 09, 2001 at 11:19 UTC
Re: HTML::Template Help
by Sifmole (Chaplain) on Aug 09, 2001 at 16:59 UTC
    Do you have it installed? If so, try "perldoc HTML::Template".

    If you are still evaluating, have you looked at the documentation on CPAN?

    The others suggestions are good as well. Hope these help get you started.

Re: HTML::Template Help
by Valkerri (Scribe) on Aug 09, 2001 at 17:52 UTC
    I just picked this up a few weeks ago, myself. I am enjoying it very much, it is so simple and elegant. I hope this is something you were looking for. Below is the skeleton of the program I worked out. (And as always, suggestions appreciated, as well)
    #!/usr/bin/perl -w use CGI qw(:standard); use HTML::Template; use strict; my $output = new CGI; my $baseDir = "/home/projects/scripts/devsimp"; my @names = $output->param; @INC = ('../cgi-lib', '/usr/lib/perl5/site_perl', '/usr/lib/perl5/5.00503'); require 'cgi-lib.pl'; ### $inputlist Gets all the stuff from the form. my $inputlist = &get_form_input(); &show_browser_output(); exit 0; sub show_browser_output { ### Based on, basically which section and what page number, ### $templatename gets the appropriate template to use for output my $templatename = &get_template(); my $a = ""; my $b = ""; ### %params gets the parts to fill in the blanks in the template ### my %params = &get_params(); my $template = HTML::Template->new(filename => "$baseDir/templates/$ +templatename"); print "Content-type: text/html\n\n"; while (($a, $b) = each(%params)) { $template->param( $a => $b ); } print $template->output; } ### Determines template to use based on subset of the form values' nam +es. ### sub get_template { ###Code to determine which template to use ### return $template; } ### Get the parts to fill in the blanks in the template ### sub get_params { my %param = (); my $page_header = ""; my $page_paragraph = ""; my $page_questions = ""; my $curr_page = $page_num; my $new_page_num = ""; ####Code to fill up the parameters that are going to be displayed ### %param = ( CAT => "$shorthand{$startingcat}", HEADER => "$page_header", PARAGRAPH => "$page_paragraph", PAGE => $new_page_num, QUESTIONS => "$page_questions", TEST_TEXT => $names[0], TEST_TEXT1 => $names[1], TEST_TEXT2 => $names[2] ); return %param; } ### Gets names of every form field submitted. ### sub get_form_input { my $listofparams = ""; my $c = ""; my @input = ""; foreach $c (@names) { $listofparams .= "$c, "; } return $listofparams; }
      This don't work:
      [jeffa@neo cgi-bin]$ perl -c val.cgi Global symbol "$template" requires explicit package name on line 51 Global symbol "$page_num" requires explicit package name on 64 Global symbol "%shorthand" requires explicit package name on line 71 Global symbol "$startingcat" requires explicit package name on line 71 val.cgi had compilation errors.
      EDIT

      How about this instead - it is an ultra simple CGI script that merely displays the names and values of the parameters from any form. First the template:

      (name this 'params.tmpl') <TMPL_IF PARAMS> <table> <tr> <tH>KEY</th> <th>&nbsp;</th> <th>VALUE</th> </tr> <TMPL_LOOP NAME=PARAMS> <tr> <td align="right"><TMPL_VAR NAME=KEY></td> <td align="center">=&gt;</td> <td><TMPL_VAR NAME=VALUE></td> </tr> </TMPL_LOOP> </table> <TMPL_ELSE> No params! </TMPL_IF>
      It is a table with the names on in the left side cell and the values in the right. If no parameters are specified, then the <TMPL_IF> will take care of the fact and gracefully continue.

      The CGI script (params.cgi):

      use CGI qw(:standard); use HTML::Template; use strict; my $template = HTML::Template->new(filename => 'params.tmpl') or die; my $params = [ map { { KEY => $_, VALUE => join(',',param($_)) } } param() ]; $template->param(PARAMS => $params); print header, start_html('Form Param Display'), $template->output, end_html;
      You can test this out by pointing any HTML form to the cgi script, or you can test it directly (YMMWV):
      http://localhost/cgi-bin/params.cgi?foo=bar&qux=baz&foo=bar2 http://localhost/cgi-bin/params.cgi

      jeffa

      Just one detail:
      To manipulate @INC, use lib ... is much better than direct assignment you used.

      pmas
      To make errors is human. But to make million errors per second, you need a computer.

Re: HTML::Template Help
by Mr.T (Sexton) on Aug 09, 2001 at 20:57 UTC
    So how would I go about installing a new module from CPAN?

    Mr.T
    qw/"I pity da foo' who don't use Perl!"/;
      Type "cpan tutorial" (without the quotes) into the "search" box in the upper-left of this window and hit Enter (or Return, as the case may be). The top two items look interesting...

      —John