in reply to HTML::Template Help

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; }

Replies are listed 'Best First'.
(jeffa) 2Re: HTML::Template Help
by jeffa (Bishop) on Aug 09, 2001 at 19:45 UTC
    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

Re2: HTML::Template Help
by pmas (Hermit) on Aug 13, 2001 at 19:50 UTC
    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.