in reply to How to use template toolkit variable in a template toolkit mysql query

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.

  • Comment on Re: How to use template toolkit variable in a template toolkit mysql query
  • Download Code

Replies are listed 'Best First'.
Re^2: How to use template toolkit variable in a template toolkit mysql query
by hamidafshar (Novice) on May 04, 2011 at 22:51 UTC

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

        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