Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Mime::Lite::TT::HTML Input Hash Name?

by doubletb (Initiate)
on Oct 04, 2011 at 14:10 UTC ( #929554=perlquestion: print w/replies, xml ) Need Help??
doubletb has asked for the wisdom of the Perl Monks concerning the following question:

I have a PERL script that successfully sends PERL messages using MIME::Lite::TT::HTML. However, I'm trying to update the email template to be more dynamic -- I want to include a FOREACH loop that loops through the elements of the hash I pass the template as TmplParams. My problem is that I have no idea what that hash of parameters is called in the template. I understand I can refer to elements of the hash by their keys, but if the keys aren't assigned until runtime, how do I pull them out? Does anyone know? I've tried VARIABLES, TmplParams, a bunch of other random things I've made up... pseudo-code below. I'm looking for what to put in the <???????> to make it work.

SCRIPT: my %emailOptions = { '<random ID>' => { 'queue' = 'Mig', 'priority' = 'high'}, '<random ID 2>' => { 'queue' = 'Inf', 'priority' = 'high'}, '<random ID 3>'=> { 'queue' = 'Mig', 'priority' = 'low'} }; my (%options); $options{INCLUDE_PATH} = '/www/some/where/' my $msg = MIME::Lite::TT::HTML->new( From => , To =>, Subject => 'Neglected Tickets', Template => { ABSOLUTE => 1, html => /path/to/TEMPLATE}, TmplOptions => \%options, TmplParams => \%emailOptions, 'X-MSMail-Priority' => 'High' ); TEMPLATE: <table> <tr> <td><b>ID</b></td> <td><b>Queue</b></td> <td><b>Priority</b></td> </tr> <tr> [% FOREACH ticket IN <??????> %] <td>[% <key> %]</td> <td>[% ticket.queue %]</td> <td>[% ticket.priority %]</td> [% END %] </tr> <table>

Replies are listed 'Best First'.
Re: Mime::Lite::TT::HTML Input Hash Name?
by keszler (Priest) on Oct 04, 2011 at 14:43 UTC

    If I'm understanding you correctly, the %emailOptions that you're passing to MIME::Lite::TT::HTML has essentially random keys, unknown until runtime. What you need is a key name with which to loop over the ticket info from %emailOptions. Can you modify %emailOptions to be:

    my %emailOptions = { tickets => [ { key => '<random ID>', 'queue' = 'Mig', 'priority' = 'high'}, { key => '<random ID 2>', 'queue' = 'Inf', 'priority' = 'high'}, { key => '<random ID 3>', 'queue' = 'Mig', 'priority' = 'low'}, ], };

    Then your template would include:

    [% FOREACH ticket IN tickets %] <td>[% ticket.key %]</td> <td>[% ticket.queue %]</td> <td>[% ticket.priority %]</td> [% END %]

      Thanks for the reply. You understand my problem perfectly, and I'll give your method a shot. It looks like it'll work, although I dislike the redundancy. I'm a little disappointed that the template isn't set up to have an accessible array/hash for those parameters -- like how PERL has @ARGV and @_ arrays for passed-in arguments...

Re: Mime::Lite::TT::HTML Input Hash Name?
by Anonymous Monk on Oct 05, 2011 at 00:20 UTC

    Is this a trick question?

    Template Toolkit, like perl, has a keys function, see Re: Catalyst DBIx Help

    And we program in Perl here, using the executable perl -- there is no PERL

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-06-24 00:38 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (555 votes). Check out past polls.