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.)