Re: [Templates] Passing a list to DBI execute?

[prev] [thread] [next] [Date index for 2004/12/06]

From: Harald Joerg
Subject: Re: [Templates] Passing a list to DBI execute?
Date: 21:34 on 06 Dec 2004
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
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] Passing a list to DBI execute?
Larry Leszczynski 18:14 on 06 Dec 2004

Re: [Templates] Passing a list to DBI execute?
Andy Wardley 18:19 on 06 Dec 2004

Re: [Templates] Passing a list to DBI execute?
Harald Joerg 21:34 on 06 Dec 2004

Re: [Templates] Passing a list to DBI execute? (solved)
Larry Leszczynski 22:55 on 06 Dec 2004

Generated at 08:56 on 15 Mar 2005 by mariachi v0.52