stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:
package PL::Template; use strict; use CGI; use HTML::Template; my $q = CGI->new; sub new { my $class = shift; my $self = {}; $self->{template}; $self->{content}; bless $self, $class; return $self; } sub format { my $self = shift; my %params = @_; $self->{content} = $params{content}; my $filter = sub { my $ref = shift; $$ref =~ s/\[aim:\/\/(.*?)\]/<a href="aim:username=$1">$1<\/a>/g; $$ref =~ s/\[google:\/\/(.*?)\]/<a href="http:\/\/www.google.com\ +/search\?q=$1">$1<\/a>/g; $$ref =~ s/\[email:\/\/(.*?)\]/<a href="mailto:$1">$1<\/a>/g; $$ref =~ s!\[([^|\]]+)\|([^\]]+?)\]!<a href=$1>$2</a>!gs; $$ref =~ s|\[([^\]]+?)\]|<a href=$1>$1</a>|g; }; my $tmpl= HTML::Template->new( filename => "tmpl/main.tmpl", associate =>$q, loop_context_vars =>1, global_vars=>1, filter => $filter ); $tmpl->param( %{ $self->{content} } ); return $q->header, $tmpl->output(); } sub output { my $self = shift; return $self->format; } 1; ##index.cgi #!perl -w use strict; use PL; my $obj = PL->new; my %content = ( title=>'hi', body=>'hello testing' ); my $page = $obj->Template->format (content => \%content); print $obj->Template->output; ##the template <html> <head> <title><!--TMPL_VAR name="title"--></title> </head> <body> <!--TMPL_VAR name="body"--> </body> </html> </readmore> <br /> I have attempted numerous methods of formatting my data into an arrayr +ef, only one of which has worked: <code> $tmpl->param( content => [{ title=>'hi'...body=>'testing' }] );
</TMPL_LOOP> In the script: $template->param(EMPLOYEE_INFO => { name => 'Sam', job => 'programmer' }, { name => 'Steve', job => 'soda jerk' }, ); print $template->output(); The output in a browser: Name: Sam Job: programmer Name: Steve Job: soda jerk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML::Template data types problem
by blokhead (Monsignor) on Apr 21, 2005 at 04:10 UTC | |
|
Re: HTML::Template data types problem
by friedo (Prior) on Apr 21, 2005 at 02:35 UTC | |
|
Re: HTML::Template data types problem
by Cody Pendant (Prior) on Apr 21, 2005 at 04:13 UTC | |
|
Re: HTML::Template data types problem
by kprasanna_79 (Hermit) on Apr 21, 2005 at 05:36 UTC | |
|
Re: HTML::Template data types problem
by stonecolddevin (Parson) on Apr 21, 2005 at 21:32 UTC |