On 3/2/2010 5:33 AM, dark0s wrote:
> I have the following code in search.cgi script:
>
>
> while (my $rset = $sth->fetchrow_arrayref) {
> push @p, $rset->[0];
> push @t, $rset->[1];
> push @l, $rset->[2];
> push @m, $rset->[3];
> }
> my $tt2 = Template->new({
> INCLUDE_PATH => ['/home/savio/templates/src',
> '/home/savio/templates/lib'],
> });
>
> my $vars = {
> p => @array1,
> t => @array2,
> l => @array3,
> m => @array4,
> };
> $tt2->process('results', $vars) || die $tt2->error();
>
> and the following template in /home/savio/template/lib/results:
>
> [% rset = [ p, t, l, m ] %]
>
> [% FOREACH value = [1..rset.size] %]
> <h5>Project: [% rset.0.p %]</h5>
> <label>Type: [% rset.1.t %]</label><br/>
> <label>language: [% rset.2.l %]</label><br/>
> <label>Members: [% rset.3.m %]</label><br/>
> [% END %]
>
> but I doesn't able to pass rset values trought $vars in template.
>
> What is the problem?
> How can I to obtain to fill template values?
The problem, to put it bluntly, is that your code makes no sense.
Fundamentally, this is not a Template::Toolkit problem. This is a
problem of understanding data structures and understanding Perl.
while (my $rset = $sth->fetchrow_arrayref) {
push @p, $rset->[0];
push @t, $rset->[1];
push @l, $rset->[2];
push @m, $rset->[3];
}
Why are you creating four separate arrays, rather than pushing the array
references onto a single array?
my $vars = {
p => @array1,
t => @array2,
l => @array3,
m => @array4,
};
Suppose each of the arrays has 2 elements. This would create a hash
that looks like this:
{
'p' => $array1[0],
$array1[1] => 't',
$array2[0] => $array2[1],
'l' => $array3[0],
$array3[1] => 'm',
$array4[0] => $array4[1],
}
I presume you actually meant to do this:
my $vars = {
p => \...@array1,
t => \...@array2,
l => \...@array3,
m => \...@array4,
};
But, the code you're showing us does not defined @array1, @array2,
@array3, or @array4. Is that elsewhere in the code, or did you mean to
use @p, @t, @l, and @m? Did you 'use strict;' at the beginning of your
script?
$tt2->process('results', $vars) || die $tt2->error();
This will make the keys of %$vars (p, t, l, and m) available as
variables in your template.
[% rset = [ p, t, l, m ] %]
You already have access to p, t, l, and m. What is the purpose of
putting them in a new array?
[% FOREACH value = [1..rset.size] %]
<h5>Project: [% rset.0.p %]</h5>
<label>Type: [% rset.1.t %]</label><br/>
<label>language: [% rset.2.l %]</label><br/>
<label>Members: [% rset.3.m %]</label><br/>
[% END %]
You're iterating over the number of elements in rset - which is always 4
- when it's clear you actually want to iterate over the number of rows
returned by the query.
Further, if p is an array reference, then rset.0 is an array reference,
so rset.0.p is meaningless.
What you should be doing is creating a single array, each element of
which contains the information from a single row returned by the query
(and each element would be better as a hash reference, not an array
reference, for easier maintainability). Then you can iterate over the
elements, printing out the information from each one.
To put it simply, you need to be aware of what data structures you're
using, how you're using them, and how you want to use them.
Ronald
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates