Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Perl CGI and Template Toolkit

by Perobl (Beadle)
on Jan 03, 2012 at 20:13 UTC ( #946124=perlquestion: print w/replies, xml ) Need Help??
Perobl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I'm trying to create an auto-generated email that will share some data with the recipient. Normally I would pass variables to TT2 using a hash ref and TmplParams:

my $msg = MIME::Lite::TT::HTML -> new( From => '', To => $to, Cc => $cc, Subject => 'Bid Strategy System - Final Pricing Approval', Template => { html => '', }, TmplOptions => \%options, TmplParams => \%mail_params, );

I can pass most of what I need to pass in %mail_params, however, I'd like to also pass data for a small table. When I pass this to standard HTML, I simply use a HoH.

TT2 EXAMPLE (displaying HoH)

[% FOREACH record IN item_hoh.values.nsort('index') %] <tr class="[% loop.parity %]"> <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 %]

I don't think I can pass a HoH using MIME::Lite::TT::HTML.

Can anyone offer me advice for dealing with a hash and a HoH? Thank you.

Replies are listed 'Best First'.
Re: Perl CGI and Template Toolkit
by Eliya (Vicar) on Jan 03, 2012 at 20:58 UTC
    I don't think I can pass a HoH using MIME::Lite::TT::HTML.

    Why do you think you can't?  What happens when you try?

      When I try to pass a reference to a HoH, it doesn't recognize it. It doesn't throw an error, but it doesn't display any of the data either.

      Also, while a bit vague, the TT2 documentation on CPAN mentions that TmplParams has to be a hash.

      It appears that this is an unfortunate limitation in MIME::Lite::TT::HTML. I'd really like to display this data using a table format, but it appears the best I can do is generate a comma delineated text field of the items in the CGI and then pass them in my existing hash.


      I mean hash reference in the above ... not hash.

        I'm pretty sure you're doing something else wrong.   As you can see below, MIME::Lite::TT::HTML is just passing through the respective hashref to TT2.  And TT2 can certainly handle HoHs (I've done it many times).

        $ pwd /home/eliya/tmp/MIME-Lite-TT-HTML-0.04 $ grep -r TmplParams . ./lib/MIME/Lite/TT/ TmplParams => \%params, ./lib/MIME/Lite/TT/ TmplParams ./lib/MIME/Lite/TT/ my $tmpl_params = delete $options->{ Tm +plParams }; $ grep -r '$tmpl_params' . ./lib/MIME/Lite/TT/ my $tmpl_params = delete $options->{ Tm +plParams }; ./lib/MIME/Lite/TT/ $tt->process( $template->{html}, $tmpl_ +params, \$html ) or croak $tt->error; ./lib/MIME/Lite/TT/ $tt->process( $template->{text}, $t +mpl_params, \$text ) or croak $tt->error;

        Could you show the code (with the HoH) you've tried?

Re: Perl CGI and Template Toolkit
by Anonymous Monk on Jan 04, 2012 at 12:05 UTC
    One rather clever trick is to pass a function.   Specifically, it can be a closure.   (Google it...)   When TT sees a template-variable it is actually smart enough to know when that “variable” is a function-reference and if so it will call that function.   A “closure” can access the local variables of whatever it was contained-in.   It can on-the-fly populate other, “real,” variables that are also referred-to in the template.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946124]
Approved by Eliya
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-07-18 22:38 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (397 votes). Check out past polls.