Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Template Toolkit and DBI Changing Table Name by Variable

by seekperlwisdom (Acolyte)
on Nov 28, 2009 at 17:43 UTC ( [id://809919]=perlquestion: print w/replies, xml ) Need Help??

seekperlwisdom has asked for the wisdom of the Perl Monks concerning the following question:

[% USE DBI %] [% DBI.connect('dbi:mysql:phones', 'user', 'password')%] This is what we have [% FOREACH fone = DBI.query('SELECT * FROM nokia') %] name : [% fone.modelno %]<br/> desc : [% fone.desc %]<br/> <img src="[% fone.image %]" width="60" height="60" alt="[% fone.mo +delno %]" /><br/> price: [% fone.price %]<br/> [% END %] <hr/>
my templatetoolkit.pl access this tt.file. i want templatetoolkit.pl to pass value of a variable which would change table name in this tt.file. i can do it with perl block but there must be a way which i cant find.i tried
[% select * from [% tablename %] %]
which is stupid but hope it make sense what i am trying to achieve with Template Toolkit. regards,

Replies are listed 'Best First'.
Re: Template Toolkit and DBI Changing Table Name by Variable
by merlyn (Sage) on Nov 28, 2009 at 18:02 UTC
    Use _ for concatenation.
    [% sql = 'SELECT * FROM ' _ tablename; FOREACH fone = DBI.query(sql) %]

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      thanks merlyn
Re: Template Toolkit and DBI Changing Table Name by Variable
by ikegami (Patriarch) on Nov 28, 2009 at 18:01 UTC
    From what I can tell from reading the docs,
    [% FOREACH fone = DBI.query("SELECT * FROM $tablename") %]
      thanks ' and " does make a big differnce i need to go back to LEARNING PERL again thanks
        You were executing a line of TT code, not a line of Perl code. Perl's rules don't apply. (Although Perl does the same thing here.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://809919]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2026-03-06 01:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.