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

Hi guys, I have a code in template toolkit that looks like:
[% FOREACH adds IN DBI.prepare('SELECT * FROM contacts WHERE account_c +ode = [% row.account_code %] AND pref_cont != 0') %] <option value='[% adds.contact_id %]'>[% adds.contact_address1 %]</opt +ion> [% END %]

This code is not working because I am using [% row.account_code %] in the query. [% row.account_code %] carries the value; "M0014400".

If I type "M0014400" manually in the query it will work and everything is fine. But I don't want that. I want to be able to use a variable like I have put in the above query. How do I do it so that it works. Please Help!!!

Replies are listed 'Best First'.
Re: How to use template toolkit variable in a template toolkit mysql query
by chromatic (Archbishop) on May 04, 2011 at 22:11 UTC

    Does this work for you?

    [% FOREACH adds IN DBI.prepare( "SELECT * FROM contacts WHERE account_code = ${row.account_code} A +ND pref_cont != 0" ) %] <option value='[% adds.contact_id %]'>[% adds.contact_address1 %]</opt +ion> [% END %]

    With that said, you're probably better off in general extracting this model behavior into a model class of some kind and leaving the view as purely formatting code. That separation of concerns tends to produce programs which are easier to develop and to maintain.

      Unfortunately no. Or maybe I am doing it not the right way.

      Here is a brief example of what I am needing to do and achieve:

      [% FOREACH row IN rows %] [% query_str = 'SELECT * FROM contacts WHERE account_code = [% row.acc +ount_code %]' %] [% END %]

      I did change [% row.account_code %] with ${row.account_code} but nothing useful happened. Do also have in mind that [% row.account_code %] will need to be wrapped in quotes as it is a string.

      Thanks for the helps, please keep them coming! :)

        As mentioned by CountZero above, you can’t nest TT that way. This should do what you want but I’m not sure it’s a good idea–

        [% FOR row IN rows %] [% query_str = "SELECT * FROM contacts WHERE account_code = ${row.acco +unt_code}" %] [% END %]

        Getting this kind of thing out of the templates will be a long run win. If you prefer to do things this way, PHP is probably just as good, if not better, for this style of in-template code. (I prefer Perl.)

        Update, as is, that code does nothing really… just sets a variable. So I don’t know it will actually do what you want. :)

Re: How to use template toolkit variable in a template toolkit mysql query
by CountZero (Bishop) on May 04, 2011 at 22:06 UTC
    My TT-foo is a bit rusty, but I think you cannot nest [% ... %] clauses.

    Did you try building the SQL string outside of the FOREACH loop and saving it in a variable and then using that variable as a parameter of the DBI.prepare?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I am a afraid it does not work!

      What I want to learn really is how to put a template variable in another variable. Like:

      [% FOREACH row IN rows %] [% query = "SELECT * FROM foo WHERE blah = [% row.account_code %] "%] [% END %]

      But this is not working. I did what you said as well Count0 but its not happening. Or maybe I haven't got the syntax right.

        I haven't tried it, but does this work?
        [% query = "'SELECT * FROM foo WHERE blah = $row.account_code '"%]

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to use template toolkit variable in a template toolkit mysql query
by CountZero (Bishop) on May 05, 2011 at 06:37 UTC
    I found this in the Template::Plugin::DBI docs:
    # single step query [% FOREACH user = DBI.query('SELECT * FROM users') %] [% user.uid %] blah blah [% user.name %] etc. etc. [% END %] # two stage prepare/execute [% query = DBI.prepare('SELECT * FROM users WHERE uid = ?') %] [% FOREACH user = query.execute('sam') %] ... [% END %] [% FOREACH user = query.execute('abw') %] ... [% END %]
    Using the '?' placeholder seems a very good idea.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to use template toolkit variable in a template toolkit mysql query
by hamidafshar (Novice) on May 05, 2011 at 08:01 UTC

    Hi guys,

    Thanks for your helps. As the final answer to my question, I am using:

    [% query = DBI.prepare("SELECT * FROM contacts WHERE account_code = '$ +{row.account_code}'") %] <tr><td><b>Pref Add</b></td><td><select id='pref_add'> [% FOREACH contacts = query.execute() %] <option value="[% contacts.contact_id %]">[% contacts.contact_title %] + [% contacts.contact_forename %] [% contacts.contact_surname %] At: [ +% contacts.contact_address1 %] [% contacts.contact_postcode %]</optio +n> [% END %] </select> </td></tr>

    WORKS GREAT,

    Thanks to all you guys.