I am attempting to build a website using the HTML::Template module to assist in abstracting the HTML out of the CGI scripts.
to build my menu I defined a template and use a template loop to iterate through the list parameters passed by $tmpl->param()
menu_bar.tmpl<DIV CLASS='menu_bar'> <tmpl_if name=menu_options> <UL CLASS='main_menu'> <tmpl_loop name=menu_options> <LI CLASS='main_menu'> <A CLASS='main_menu' HREF='<tmpl_var name=option_address>' > <tmpl_var name=option_name> </A> </LI> </tmpl_loop> </UL> </tmpl_if> </div>
Then in the code I use the $tmpl->param() function and pass it my list of links.
#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use HTML::Template; my $cgi = new CGI; my $tmpl = new HTML::Template( path => [ '/path/to/' ], filename => menu_bar.tmpl, ); $tmpl->param( [ { option_name => 'Home', optopn_address => '/index.cgi', }, { option_name => 'About', option_address => '/about.cgi', }, { option_name => 'Contact', option_address => '/contact.cgi', }, ] ); print $cgi->header(); print $tmpl->output();
I think this code could be made cleaner using a list. My first thought was to use a hash and a loop, but that won't work because it wont come out in the correct order.
and replace them with one statement.{ option_name => "$option", option_address => "$address", }
This was my hair-brained idea... Needless to say: syntax-error...
Thoughts?my @options = ("Home", "About", "Contact",); my %options_menu = ( Home => '/index.cgi', About => '/about.cgi', Contact => 'contact.cgi', ); $tmpl->param( menu_options => [ foreach(@options){ { option_name => "$_", option_link => "$menu_options{$_}", }, } ], );
In reply to HTML::Template Maintainability by PyrexKidd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |