Larry Leszczynski writes:
I'm having trouble passing a list of bind parameter values to the DBI
execute function, hoping someone can shed some light. It works fine when
the query has a single placeholder and I send a single string. But when
the query has multiple placeholders and I try to send a list, the execute
subroutine is just getting the array ref and not the list of values, e.g.
something like this fails:
[%
query = "select * from users where email like ? and status = ?";
bind_params = [ '%larryl%', 'A' ];
PROCESS my_utility_template;
%]
where "my_utility_template" is something like:
[%
sth = DBI.prepare(query);
rows = sth.execute(bind_params);
FOREACH row IN rows;
...do stuff...
END;
-%]
Passing the params explicitly works, e.g.:
rows = sth.execute('%larryl%', 'A');
except I can't do that in my case, because they're defined elsewhere and
used later within the template that does the prepare/execute.
Is there some sort of dereferencing or eval-ing that I need to do to get
the bind_params list passed through to execute?
You can indeed use TT2's eval filter to dereference a list.
Here's a proof of concept:
-------------------------------------------------------------------
#!perl -w
# This should work to dereference a TT2 list. It prints
# --------------------
# Params: %larryl%,A
# --------------------
use strict;
package STH;
sub new {bless {},'STH';}
sub execute {my $self = shift; print "Parameters: ",join ", ",@_};
package main;
use Template;
my $t = Template->new;
$t->process(\*DATA,{'sth'=> STH->new});
__DATA__
[%- bind_params = ['%larryl%', 'A'] %]
[%- starttag = '[' _ '%'; endtag = '%' _ ']' %]
[%- FILTER eval %]
[%- starttag %]
rows = sth.execute('[% bind_params.join("','")%]')
[%- endtag %]
[%- END -%]
-------------------------------------------------------------------
Now I think it's *really* obvious why we are all waiting for TT3 :-)
--
Cheers,
haj
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates