in reply to HTML::Template and hash o' links+labels

Here's my usual method (important parts only):
my %urlhash = ( b('home') => '/', b('sitedocs') => '/sitedocs.pl', b('switches') => '/switches.pl', b('netdocs') => '/netdocs.pl', ); my @urls; while (my ($name,$url) = each %urlhash) { push @urls, { NAME => $name, URL => $url }; } $template->param( URLLOOP => \@urls ); __END__ <tmpl_loop name="urlloop"> <a href="<tmpl_var name="url">"> <tmpl_var name="name"> </a> </tmpl_loop>

Replies are listed 'Best First'.
Re: (2) HTML::Template and hash o' links+labels (Graci)
by ybiC (Prior) on Dec 21, 2000 at 10:11 UTC
    Thanks, repson - is just what I was looking for.   8^)
    Working code + markup below.
        cheers,
        Don
        striving for Perl Adept
        (it's pronounced "why-bick")


    (template.pl)

    #!/usr/bin/perl -wT use strict; use HTML::Template; use HTML::Entities; use CGI qw(:all); # use CGI::Pretty qw(:all); use CGI::Carp qw(fatalsToBrowser); # debugging only - for production +"use CGI::Carp;" use Time::localtime; use File::stat; use vars qw(@urlsA @urlsB); my $code = 'template.pl'; my $tmpl = 'template.tmpl'; my $template = HTML::Template->new(filename => "$tmpl"); my %urlhashA = ( 'home' => '/', 'sitedocs' => '/doc/', ); my %urlhashB = ( 'Perl Monks' => 'http://www.perlmonks.org/', 'CPAN' => 'http://search.cpan.org/', 'Google' => 'http://www.google.com/', 'Cisco' => 'http://www.cisco.com/', ); while (my ($name,$url) = each %urlhashA) { push @urlsA, { nameA => $name, urlA => $url, } } $template->param(urlloopA => \@urlsA); while (my ($name,$url) = each %urlhashB) { push @urlsB, { nameB => $name, urlB => $url, } } $template->param(urlloopB => \@urlsB); $template-> param( codemod => ctime(stat($code)-> mtime), tmplmod => ctime(stat($tmpl) -> mtime), servname => $ENV{'SERVER_NAME'}, ); print header, $template->output;


    (template.tmpl)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE><!-- tmpl_var name=servname --></TITLE> </HEAD> <BODY> <!-- tmpl_loop name="urlloopA" --> <A HREF="<!-- tmpl_var name="urlA" -->"> <B><!-- tmpl_var name="nameA" --></B> </A> &nbsp;&nbsp;&nbsp; <!-- /tmpl_loop --> <P> <!-- tmpl_loop name="urlloopB" --> <A HREF="<!-- tmpl_var name="urlB" -->"> <!-- tmpl_var name="nameB" --> </A> <BR> <!-- /tmpl_loop --> <P> Code update <!-- tmpl_var name=codemod --> <BR> Template update <!-- tmpl_var name=tmplmod --> </BODY> </HTML>
Re: Re: HTML::Template and hash o' links+labels
by maverick (Curate) on Dec 21, 2000 at 07:55 UTC
    I've been using HTML::Template on the project I'm currently working on. This is exactly the method I use and it has worked like a charm.

    /\/\averick