Email::MIME::Kit::Renderer::MicroMason - Render parts of your mail with Text::MicroMason


Email-MIME-Kit-Renderer-MicroMason documentation Contained in the Email-MIME-Kit-Renderer-MicroMason distribution.

Index


Code Index:

NAME

Top

Email::MIME::Kit::Renderer::MicroMason - Render parts of your mail with Text::MicroMason

SYNOPSIS

Top

To use MicroMason in your mkit use something like:

    {
      "renderer": "MicroMason",
      "header": [
        { "From": "WY Corp <noreplies@wy.example.com" },
        { "To": "<% $ARGS{recruit}->email %>" },
        { "Subject": "Welcome aboard, <% ARGS{recruit}->name %>" }
      ],
      "alternatives": [
        { "type": "text/plain", "path": "body.txt" },
         {
          "type": "text/html",
          "path": "body.html",
          "container_type": "multipart/related",
          "attachments": [ { "type": "image/jpeg", "path": "logo.jpg" } ]
        }
      ]
    }

Then in your email templates (body.txt and body.html) you can do:

    <%args>
    $recruit
    $cid_for
    </%args>

    <& "../includes/header.msn", %ARGS &>

    <p>
    Dear <% $recruit->name %>,
    </p>

    <p>
    Welcome to WY Corp.
    </p>

    <& "../includes/footer.msn", %ARGS &>

EMK::Renderer::MicroMason will try to make any components included with <& ... &> relative to the mkit directory.

DESCRIPTION

Top

This renderer for Email::MIME::Kit uses Text::MicroMason to enable you to write your mkits using basic Mason syntax. See Text::MicroMason::HTMLMason for details on the syntax.

This is based on Text::MicroMason rather than the full blown HTML::Mason because HTML::Mason is focused on directories and files and Email::MIME::Kit prefers to work with strings. Text::MicroMason accommodates this and is a bit smaller than it's big brother.

METHODS

render()
    render( $content_ref, $stash )

Called by Email::MIME::Kit::Renderer to parse template strings ($content_ref) with Text::MicroMason and return a plain text string.

SEE ALSO

Top

Email::MIME::Kit, HTML::Mason, Text::MicroMason, and Text::MicroMason::HTMLMason.

AUTHOR

Top

Mark V. Grimes, <>

COPYRIGHT AND LICENSE

Top


Email-MIME-Kit-Renderer-MicroMason documentation Contained in the Email-MIME-Kit-Renderer-MicroMason distribution.
package Email::MIME::Kit::Renderer::MicroMason;

use Moose;
with 'Email::MIME::Kit::Role::Renderer';
use Text::MicroMason;
use Cwd;
use Carp;

our $VERSION = '1.002';

sub render {
    my ( $self, $content_ref, $stash ) = @_;
    $stash ||= {};

    # use Data::Dump; dd @_;

    # Change to the mkit dir so any components (eg: <& ... &>) can
    # be specified as paths relative to the mkit dir
    my $orig_dir;
    if ( -d $self->kit->source ) {
        $orig_dir = getcwd();
        chdir $self->kit->source
          or carp "Couln't change to mkit dir: @{[ $self->kit->source ]}";
    }

    # Parse the template with Text::MicroMason
    my $outbuf = $self->mason->execute( text => $$content_ref, %$stash );

    # Return to the original directory
    chdir $orig_dir if $orig_dir;

    # ddx $outbuf;
    return \$outbuf;
}

has mason => (
    is => 'ro',
    ## isa      => 'Text::MicroMason',  # T:MM isa strange subclass
    lazy     => 1,
    init_arg => undef,
    default  => sub {
        Text::MicroMason->new(-Filters);
    },
);

no Moose;
1;