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

Hi Monks!

I am trying to process a MySQL query and send the results to the browser using Template Toolkit. It is my understanding that TT2 will only handle lists and hashes. I have opted to use a hash.

My query requires that I pass several records of data to the browser. I am trying to loop fetchrow_array() to to process each record to see if there is a match (my query includes a filtered search). If there is a match, I need to retrieve several columns of data from that record. So I have multiple pieces of data and multiple records to deal with.

Normally, I would format and print these results to the browser as part of the query. However, TT2 requires me to send this information to the browser via a template (as I'm sure you know). So it seems like I have to build a hash. I can write code to do this using all the scalar data, but I'm wondering if there is an easier (preferred) way to process this query. Seems like a lot of work (especially when TT2 makes so many other things so easy to do). Did I mention I have little experience using TT2?

What is the best way to do this?

Thanks for your time.

  • Comment on Best way to send records to the browser?

Replies are listed 'Best First'.
Re: Best way to send records to the browser?
by Your Mother (Archbishop) on Jun 26, 2010 at 05:10 UTC

    You could pass the $sth (statement handle) to the template and do the iteration in the template. It'll take objects and things as well as regular data.

      Thanks for the help.

      I'm really trying to keep my code out of my templates. In fact, that is why I opted to use TT2 in the first place. I have a web designer working on this project who doesn't understand TT2 or Perl.

      It appears that other template tools (like Template::HTML) allow you to pass arrays to templates. I have written code code that basically builds an array of hashes. Its fairly compact. This would work for Template::HTML, but not for TT2.

      I think I have to create a hash of hashes. Unfortunately, I'm not sure how to do this syntactically - and this is where I could use a little help.

      Am I heading down the right path? My thought is to create a hash for each relevant record in the table. Then, I can create a hash of hashes to encompass all of the records. However, if I send the HoH to TT2, will it understand it? This is uncharted territory for me. Also, I'm not sure how the syntax works for building a HoH in Perl. I'm still researching this, but haven't figured it out (help appreciated).

      I guess the other thing I could do is take my existing array of hashes and parse it with Perl to build one large hash. I'm sure this would work (I've done something similar in the past) but one would think there exists an easier (preferred) way to do all of this. Hence my post here for the Monks.

      I really appreciate any further guidance you experts can offer me.

      :-)

        This would work for Template::HTML, but not for TT2.

        Of course it would. You can pass arbitrary perl data structures to TT and its loop controls are better/deeper than HTML::Template's. The reason some people prefer HT over TT is precisely because it has drastically fewer features (and better speed but for the template layer, that's generally the dumbest place to try to optimize) so it is closer to the purist idea of a view; which, as you mention, can be better for the designer, etc. There is nothing HT does that you can't do in TT.

        Why don't you post some sample code of what your data structure looks like?