Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Perl CGI and Template Toolkit

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

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

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.

Replies are listed 'Best First'.
Re^3: Perl CGI and Template Toolkit
by Eliya (Vicar) on Jan 03, 2012 at 21:38 UTC

    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?

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


        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 => '', To => '', Subject => 'Bid Strategy System - Final Pricing Approval', Template => { html => '', }, TmplParams => $HoH, );

        where 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? ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946138]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2018-06-21 22:47 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.