Email-MIME-Kit-Renderer-MicroMason

 view release on metacpan or  search on metacpan

lib/Email/MIME/Kit/Renderer/MicroMason.pm  view on Meta::CPAN

package Email::MIME::Kit::Renderer::MicroMason;

# ABSTRACT: Render parts of your mail with Text::MicroMason

use Moose;
with 'Email::MIME::Kit::Role::Renderer';
use Scalar::Util ();
use Text::MicroMason;
use Carp;

our $VERSION = '1.21';

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

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

    return \$outbuf;
}

has mason => (
    is => 'ro',
    ## isa      => 'Text::MicroMason',  # T:MM isa strange subclass
    lazy     => 1,
    init_arg => undef,
    default  => sub {
        my ($self) = @_;
        my $mason = Text::MicroMason->new('-Filters', '-MKit');
        $mason->{__mkit_renderer} = $self;
        Scalar::Util::weaken($mason->{__mkit_renderer});
        return $mason;
    },
);

{
  $INC{'Text/MicroMason/MKit.pm'} = 1;
  package Text::MicroMason::MKit;

  sub read_file {
    my ($self, $file) = @_;
    my $text = $self->{__mkit_renderer}->kit->get_kit_entry($file);
    return $$text;
  }
}

no Moose;
1;

__END__

=pod

=head1 NAME

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

=head1 VERSION

version 1.21

=head1 SYNOPSIS

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 &>



( run in 1.190 second using v1.01-cache-2.11-cpan-5b529ec07f3 )