in reply to Template::Toolkit, DBI and mysql % wildcards

Why don't you simply move the query out of the TT template?

my $dbh = DBI->connect( ... ); my $items = $dbh->selectall_arrayref(q{ select user from logs where user not like 'adm%' }, {Slice=>{}}); my $template = Template->new or die Template->error; $template->process(\*DATA, {items => $items}) or die $template->error; __DATA__ [% FOREACH item = items %] [% item.user %] [% END %]

UPDATE: I should have checked before posting, holli gave the same answer first. holli++

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Template::Toolkit, DBI and mysql % wildcards
by holli (Abbot) on Mar 14, 2006 at 21:07 UTC
    Yes in this case. But I often use it the DBI plugin like
    [% USE DBI('dbi:mysql:logs','','') %] [% SET dbh = dbi.prepare(query) %] [% FOREACH user = user_list %] [% FOREACH item = dbh.execute(user) %] [% item.user %] [% END %] [% END %]
    code:
    $template->process(\$tt, {query => $query, user_list => [foo, bar]) or + die $template->error(); [download]
    That's quite handy. Also when you put all the database stuff inside the templates, you can easily switch between databases, without touching the perl code.


    holli, /regexed monk/
Re^2: Template::Toolkit, DBI and mysql % wildcards
by bfdi533 (Friar) on Mar 14, 2006 at 21:13 UTC

    I tried that first but did not know enough about getting the results into a hash do it this way.

    I will say your code for later use; very helpful!