Re: [Templates] Passing a list to DBI execute? (solved)
[prev]
[thread]
[next]
[Date index for 2004/12/06]
Thanks for the idea Harald! I had previously tried using eval but without
success. It now works using the following:
[%
sth = DBI.prepare(query);
params_list = "'" _ bind_params.join("','") _ "'";
"[% rows = sth.execute($params_list) %" _ "]" FILTER eval;
FOREACH row IN rows;
...do stuff...
END;
-%]
The piece I was missing was splitting the end tag so that the parser does
not think there are nested tags going on.
Larry Leszczynski
On Mon, 6 Dec 2004, Harald Joerg wrote:
> Larry Leszczynski writes:
>
> > [%
> > sth = DBI.prepare(query);
> > rows = sth.execute(bind_params);
> > FOREACH row IN rows;
> > ...do stuff...
> > END;
> > -%]
> >
[...]
> >
> > 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
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates