Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Perl CGI and Template Toolkit

by Perobl (Beadle)
on Jan 03, 2012 at 21:49 UTC ( #946144=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Perl CGI and Template Toolkit
in thread Perl CGI and Template Toolkit

I've done it in TT2 many times too, but in this case I'm using MIME::Lite::TT::HTML. Have you actually tried to pass a HoH ref using this module?

I know my HoH ref is good because I've tested it in the CGI. The resultant HoH ref doesn't work. The hash ref does work.

If you're sure it can be done, and you can show me a working example of how it can be done, i'll buy you lunch :)

Thanks.


Comment on Re^4: Perl CGI and Template Toolkit
Re^5: Perl CGI and Template Toolkit
by Eliya (Vicar) on Jan 03, 2012 at 22:47 UTC

    Ok, here you are.

    use MIME::Lite::TT::HTML; my $HoH = { item_hoh => { row1 => { index => 99, col1 => "Col 1", col2 => "Col 2" }, row2 => { index => 42, col1 => "Col 1", col2 => "Col 2" }, } }; my $msg = MIME::Lite::TT::HTML -> new( From => 'donotreply@windowsintowaukesha.com', To => 'you@foo.bar', Subject => 'Bid Strategy System - Final Pricing Approval', Template => { html => 'email_bid_strategy_final_appr.tt', }, TmplParams => $HoH, );

    where email_bid_strategy_final_appr.tt is

    [% FOREACH record IN item_hoh.values.nsort('index') %] <tr> <td>[% record.index %]</td> <td>[% record.col1 %]</td> <td>[% record.col2 %]</td> </tr> [% END %]

    To show that the correct output is produced, I've added a line to print out $html in the module's code:

    ... $tt->process( $template->{html}, $tmpl_params, \$html ) or croak $ +tt->error; print STDERR $html; # <--- ...

    and the output is (as expected):

    <tr> <td>42</td> <td>Col 1</td> <td>Col 2</td> </tr> <tr> <td>99</td> <td>Col 1</td> <td>Col 2</td> </tr>

    Now where's my lunch? ;)

      Thank you so much Eliya! I tried your code and it works great. You can pass a HoH to MIME::Lite::TT::HTML, I stand corrected :).

      I thought I understood hash usage with TT2, but apparently I'm missing something. Could you possibly help me a bit more?

      The data I wish to share in my email is queried from a DB:

      $sth = $dbh -> prepare("SELECT * FROM bs_trans WHERE bsid = ?"); $sth -> execute($bsid);

      I then generate my HoH using:

      my $i = 0; while (@rray3 = $sth -> fetchrow_array()) { $item_hoh{$i} => {index => $i, mva => $rray3[3], hv1 => $rray3[5], + lv1 => $rray3[7], ltc => $rray3[10], qty => 'tbd'}; $i++; };

      I then prepare my email:

      my $msg = MIME::Lite::TT::HTML -> new( From => 'donotreply@blahblah.com', To => $to, Cc => $cc, Subject => 'Bid Strategy System - Final Pricing Approval', Template => { html => 'email_bid_strategy_final_appr.tt', }, TmplOptions => \%options, TmplParams => \%item_hoh, ); $msg -> send();

      Notice that I'm passing the hash reference "\%item_hoh" to TT2 via TmplParams whereas you passed $HoH. I need to generate my HoH on the fly. While your code works for a hard coded HoH, mine doesn't and I don't understand why.

      Again, in TT2, I'm attempting to display the results this way:

      [% FOREACH record IN item_hoh.values.nsort('index') %] <tr> <td>[% record.index %]</td> <td>[% record.mva %]</td> <td>[% record.hv1 %]</td> <td>[% record.hv2 %]</td> <td>[% record.ltc %]</td> <td>[% record.qty %]</td> <tr> [% END %]

      None of the data is displayed in my emails when I use this code ... and I don't understand why. Thank you so much, I really appreciate your help, and I would be happy to buy you lunch because I'd really like to include this data in a table format in my emails :D.

        When you refer to something like item_hoh in the template, this is a key in the hash, not the name of the hash itself (TT2 knows nothing about the name — the hashref could also be anonymous).  This isn't any different from using flat hashes to define/set TT2 variables, btw.

        The solution is to just wrap another hash around it, e.g. an anonymous one (in my example above I used a named one, $HoH):

        ... TmplParams => { item_hoh => \%item_hoh },

        The curlies create an anonymous hash(ref), and the value of the entry keyed by "item_hoh" is the ref to your hash %item_hoh.  The name of the latter is irrelevant.  In case you need to pass more top-level items (like the item_hoh), just add further entries in the anon hash.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (18)
As of 2014-07-30 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls