Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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 => 'donotreply@windowsintowaukesha.com', To => $to, Cc => $cc, Subject => 'Bid Strategy System - Final Pricing Approval', Template => { html => 'email_bid_strategy_final_appr.tt', }, 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.

Comment on Perl CGI and Template Toolkit
Select or Download Code
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.

      EDIT

      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/HTML.pm: TmplParams => \%params, ./lib/MIME/Lite/TT/HTML.pm:=head2 TmplParams ./lib/MIME/Lite/TT/HTML.pm: my $tmpl_params = delete $options->{ Tm +plParams }; $ grep -r '$tmpl_params' . ./lib/MIME/Lite/TT/HTML.pm: my $tmpl_params = delete $options->{ Tm +plParams }; ./lib/MIME/Lite/TT/HTML.pm: $tt->process( $template->{html}, $tmpl_ +params, \$html ) or croak $tt->error; ./lib/MIME/Lite/TT/HTML.pm: $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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2014-10-01 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (18 votes), past polls