[cgiapp] Re: Proper Way To Load Template Params

adam at spatialsystems.org adam at spatialsystems.org
Wed Mar 18 15:30:18 EDT 2009


>> 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.


Thanks








More information about the cgiapp mailing list