punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:

Mylar Monks,

So I'm using Template Toolkit to output dynamic web pages. So far so good.

I'm adding a "email me my profile info" button. When clicked on, I want to retrieve the user's profile info from teh db, populate an email template, send it to the user via sendmail, then populate a confirmation template and serve it to the user saying "ths info was just emailed to you...".

So I tried ( in pseudo code):

$vars = { image_path => $image_path, data => \%data, }; $subject = "contact info"; $template_file = 'contact_info_email.htm'; $template_1->process($template_file, $vars) || die "Template process failed: ", $template->error(), "\n"; &send_mail_html($email, $from_address, $subject, $template_1); $template_file = 'confirm_contact_info_email.htm'; $template->process($template_file, $vars) || die "Template process failed: ", $template->error(), "\n";
Where &send_mail_html contacins, among other things, the usual:
$to_address = $_[0]; $from = $_[1]; $subject = $_[2]; $content = $_[3]; ..... print MAIL "To: print MAIL "From: $from\n"; print MAIL "Subject: $subject\n"; print MAIL "$content\n";
And it almost works. The user gets an email, and a confirmation page. But with two problems:
  1. The email contents says: "Template=HASH(0x84fbe20)", instead of containing the template
  2. The confirmation page contains both the templates, the email template AND the confirmation template

So, I'm clearly doing somethign wrong. How should I be doing this?

Thanks.




Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Template Toolkit: Output one template to Sendmail, another to browser?
by EvanCarroll (Chaplain) on Aug 26, 2007 at 06:50 UTC
    Try in the Template Toolkit USE Dumper; Dump ( $content ); Your code:
    $template_1->process($template_file, $vars) || die "Template process failed: ", $template->error(), "\n"; &send_mail_html($email, $from_address, $subject, $template_1); $template_file = 'confirm_contact_info_email.htm'; $template->process($template_file, $vars) || die "Template process failed: ", $template->error(), "\n";
    This is what process is supposed to do, from docs:
    By default, the processed template output is printed to STDOUT. The process() method then returns 1 to indicate success. A third parameter may be passed to the process() method to specify a different output location. This value may be one of: a plain string indicating a filename which will be opened (relative to OUTPUT_PATH, if defined) and the output written to; a file GLOB opened ready for output; a reference to a scalar (e.g. a text string) to which output/error is appended; a reference to a subroutine which is called, passing the output as a parameter; or any object reference which implements a 'print' method (e.g. IO::Handle, Apache::Request, etc.) which will be called, passing the generated output as a parameter.

    So when you send template1 to send_mail you're actually sending a blessed template object (hash). And, your double output is presumably the confirmation screen reading from STDIN, (which your writing to twice, because your not sending process a third var)

    On top of that

    • prefixing the func with '&' should be thought of as deprecated in non-reference notation and has a side effect if you aren't explicit about arguments.
    • I'd use a module for this
    • use parallel assignment  my ( $to_address, $from, $subject, $content ) = @_;



    Evan Carroll
    www.EvanCarroll.com
      Thanks.

      I like the sounds of this:

      A third parameter may be passed to the process() method to specify a different output location. This value may be one of: ..... a reference to a scalar... to which output/error is appended;

      So if I understand this correctly, I can do

      $template->process($template_file, $vars, $email_content) || die "Template process failed: ", $template->error(), "\n";
      And TT will send the output to $email_content instead of to STDOUT. Then I can
      send_html_mail($to, $from, $subject, $email_content);
      Yes?
        You've almost got it. Your call to process should be:
        $template->process($template_file, $vars, \$email_content) || die "Template process failed: ", $template->error(), "\n";
        Notice the slash before $email_content. It creates a scalar reference that the manual says is required for the behaviour that you want.
        --
        Clayton
      The way the process method works always annoyed me, so I wrote a little wrapper around it (the $template object needs to be global in this version):
      sub parse_template($;$) { my $filename = shift; my $vars = shift || {}; my $parsed; $template->process($filename, $vars, \$parsed) or die 'Couldn\'t o +pen template: ' . $template->error(); return $parsed; }
Re: Template Toolkit: Output one template to Sendmail, another to browser?
by ides (Deacon) on Aug 26, 2007 at 14:58 UTC

    You can solve your problem by using MIME::Lite::TT or MIME::Lite::TT::HTML, check out my short article on how they work.

    Hope it helps...

    Frank Wiles <frank@revsys.com>
    www.revsys.com