in reply to Re: How to use template toolkit variable in a template toolkit mysql query
in thread How to use template toolkit variable in a template toolkit mysql query

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! :)

Replies are listed 'Best First'.
Re^3: How to use template toolkit variable in a template toolkit mysql query
by Your Mother (Archbishop) on May 04, 2011 at 23:06 UTC

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

      Absolutely greatful *Your Mother*, *CountZero* and *Chromatic*. Below worked for me:
      [% FOR row IN rows %] [% query_str = "SELECT * FROM contacts WHERE account_code = ${row.acco +unt_code}" %] [% END %]
      I had tried this previously but did not use double quotes - duhhhhhhhhh :-|. Thanks alot you guys.
      This works