in reply to Template Toolkit: Output one template to Sendmail, another to browser?

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



Evan Carroll
www.EvanCarroll.com

Replies are listed 'Best First'.
Re^2: Template Toolkit: Output one template to Sendmail, another to browser?
by punch_card_don (Curate) on Aug 26, 2007 at 13:47 UTC
    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
        Yep, that did it. Got me email template by email, and my confirmation template returned to browser.

        Thanks.

Re^2: Template Toolkit: Output one template to Sendmail, another to browser?
by Cap'n Steve (Friar) on Aug 27, 2007 at 01:45 UTC
    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; }