[cgiapp] Re: Proper Way To Load Template Params
P Kishor
punk.kish at gmail.com
Thu Mar 19 10:01:10 EDT 2009
On Wed, Mar 18, 2009 at 2:30 PM, <adam at spatialsystems.org> wrote:
>>> My template is getting data via a SQL query. There is exactly one row
>>> retrieved.
>>>
>>> Currently I use the below method to show all records using a loop:
>>> $template->param(
>>> RESULTS => $self->dbh->selectall_arrayref('
>>> SELECT age, day FROM table WHERE id = ?',
>>> { Slice => {} },
>>> $self->session->param('cell')->{'sid'} )
>>> );
>>>
>>>
>>> Template:
>>> <TMPL_LOOP NAME="RESULTS">
>>> <TMPL_VAR NAME="age"><TMPL_VAR NAME="day">
>>> </TMPL_LOOP>
>>
>>This is not directly related, but you may want to look at DBIx::Simple
>>in combination with its support for DBIx::Interp as a simpler, interface
>>over top of raw DBI.
>>
>>> But how should I load the template if I have exactly one row returned?
>>> I could fudge it and use the loop to iterate of the one row, but I think
>>> that may be a sub optimal method.
>>>
>>> $self->dbh->selectall_arrayref('
>>> SELECT age, day FROM table WHERE id = ?',
>>> { Slice => {} },
>>> $self->session->param('cell')->{'sid'} )
>>
>>To get one row back, use "selectrow_hashref", it will return a single hashref.
>>( no "Slice" needed. )
>>
>>You then can skip the loop tokens in a template:
>>
>><TMPL_VAR NAME="age"><TMPL_VAR NAME="day">
>>
>>
>>
>>Mark
>
>
> $template->param(
> TITLE => "Page Title",
> RESULTS => $self->dbh->selectall_arrayref('
> SELECT age, day FROM table WHERE id = ?',
> { Slice => {} },
> $self->session->param('cell')->{'sid'} )
> );
>
>
> So how would the above line be modified for the selectrow_hashref to
> work? I've been trying it on my own and I either end up calling
> $template with odd paramaters or I'm missing my bind variables for the
> SQL.
>
I do like so
my $results = $sth->fetchall_arrayref({});
and then
my $tmpl = param(
RESULTS => $results,
);
>
> Thanks
>
>
>
>
>
>
>
> ##### CGI::Application community mailing list ################
> ## ##
> ## To unsubscribe, or change your message delivery options, ##
> ## visit: http://lists.openlib.org/mailman/listinfo/cgiapp ##
> ## ##
> ## Web archive: http://lists.openlib.org/pipermail/cgiapp/ ##
> ## Wiki: http://cgiapp.erlbaum.net/ ##
> ## ##
> ################################################################
>
>
--
Puneet Kishor http://www.punkish.org/
Nelson Institute for Environmental Studies http://www.nelson.wisc.edu/
Carbon Model http://carbonmodel.org/
Open Source Geospatial Foundation http://www.osgeo.org/
More information about the cgiapp
mailing list