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.


I would like to get rid of all of the individual entries of:
{ option_name => "$option", option_address => "$address", }
and replace them with one statement.
I believe this will make future maintainability easier.
can someone please point me in the right direction.
Thanks.

EDIT:

This was my hair-brained idea... Needless to say: syntax-error...

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{$_}", }, } ], );
Thoughts?

In reply to HTML::Template Maintainability by PyrexKidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.