Perl/MySQL/TT
Wilson, Andrew (Belfast)
Andrew.Wilson at trw.com
Mon Oct 29 09:38:55 CST 2001
> -----Original Message-----
> From: Tony Bowden [mailto:tony at kasei.com]
> Sent: Monday, October 29, 2001 2:27 PM
> To: belfast-pm at pm.org
> Subject: Re: Perl/MySQL/TT
>
>
> On Mon, Oct 29, 2001 at 01:55:46PM +0000, Scott McWhirter wrote:
> > >Can anyone give me a few pointers as to how to
> > >get the data passed to the template in the right way?
> >
> > Here is a way i do it:
>
> You are on drugs, aren't you.
Yeah, I was going to post a solution to the overall script, now I think
I'll restrict myself to pointing out glaring style errors
>> --------------------------------------------------
>> ######################################################
>> # $fds is a hash reference of rows of data in a table
>> ######################################################
>> foreach my $d (sort keys %{$fds}){
>> my $name = $fds->{"$d"}{"name"};
>> my $pic = $fds->{"$d"}{"pic"};
>> my $dob = $fds->{"$d"}{"dob"};
>>
>> my $vars = {
>> name => $name,
>> pic => $pic,
>> dob => $dob
>> };
>>
>> ###############################################
>> # $t->template_by_name(); gets a template from
>> # a table.
>> ###############################################
>> my $template = $t->template_by_name('fritab');
>>
>>
>> ###############################################
>> # $t->give_me_html(); gives me html from the
>> # processed template with $vars
>> ###############################################
>> $html .= $t->give_me_html($template,$vars);
>> }
>> --------------------------------------------------
>>
>> the template(fritab) for example is just:
>> >>[% name %] birthdate is [% dob %] and this is what they look like
>> <img src="[% pic %]"><br><<>
That's a phenomonally bad way to do that. Here are two betters ways
1) What's all that messing about constructing a hash ref you already
have all about then?
foreach my $d (sort keys %{$fds}) {
my $vars = $fds->{$d}
# ... the rest of your solution goes here
}
2) better still do it all in the template
my @data = map {$fds->{$_}} sort keys %{$fds};
my $template = $t->template_by_name('fruitab');
my $html = $t->give_me_html($template, @data);
the give_me_html looks very like tony's process template and the template
looks like
[% foreach p = people %]
>>[% p.name %] birthdate is [% p.dob %] and this is what they look like
<img src="[% p.pic %]"><br><<
[% END %]
Note: I don't have perl here and haven't tried compiling these, they may
have
syntax errors.
Of course the Class:DBI solution propounded by Tony is better again.
cheers
Andrew
More information about the Belfast-pm
mailing list