Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

HTML::Template Maintainability

by PyrexKidd (Monk)
on Jan 06, 2011 at 00:52 UTC ( [id://880724]=perlquestion: print w/replies, xml ) Need Help??

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

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?

Replies are listed 'Best First'.
Re: HTML::Template Maintainability
by GrandFather (Saint) on Jan 06, 2011 at 01:21 UTC

    Your template uses a loop to build the list so your parameter list needs an entry for the loop containing a list of the name/value pairs used to populate the list entries. Consider:

    #!/usr/bin/perl use strict; use warnings; use HTML::Template; my $html = <<HTML; <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> HTML my @options = ("Home", "About", "Contact",); my %options_menu = ( Home => '/index.cgi', About => '/about.cgi', Contact => 'contact.cgi', ); my $tmpl = HTML::Template->new ( path => ['/path/to/'], scalarref => \$html, ); my %params; push @{$params{menu_options}}, {option_name => $_, option_address => $options_menu{$_}} for @options; $tmpl->param (%params); print $tmpl->output ();

    Prints:

    <DIV CLASS='menu_bar'> <UL CLASS='main_menu'> <LI CLASS='main_menu'> <A CLASS='main_menu' HREF='/index.cgi'>Home</A> </LI> <LI CLASS='main_menu'> <A CLASS='main_menu' HREF='/about.cgi'>About</A> </LI> <LI CLASS='main_menu'> <A CLASS='main_menu' HREF='contact.cgi'>Contact</A> </LI> </UL> </div>
    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://880724]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found