File Coverage

blib/lib/Email/MIME/Kit/Renderer/Text/Template.pm
Criterion Covered Total %
statement 22 22 100.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 5 5 100.0
pod 0 1 0.0
total 33 39 84.6


line stmt bran cond sub pod time code
1             package Email::MIME::Kit::Renderer::Text::Template 1.101304;
2 1     1   69217 use Moose;
  1         3  
  1         5  
3             with 'Email::MIME::Kit::Role::Renderer';
4             # ABSTRACT: render parts of your mail with Text::Template
5              
6 1     1   5673 use Module::Runtime ();
  1         3  
  1         290  
7              
8             sub _enref_as_needed {
9 8     8   19 my ($self, $hash) = @_;
10              
11 8         13 my %return;
12 8         32 while (my ($k, $v) = each %$hash) {
13 21 50 33     107 $return{ $k } = (ref $v and not blessed $v) ? $v : \$v;
14             }
15              
16 8         17 return \%return;
17             }
18              
19             #pod =attr template_class
20             #pod
21             #pod This attribute stores the name of the class that will be standing in for
22             #pod Text::Template, if any. It defaults, obviously, to Text::Template.
23             #pod
24             #pod =cut
25              
26             has template_class => (
27             is => 'ro',
28             isa => 'Str',
29             default => 'Text::Template',
30             );
31              
32             #pod =attr template_args
33             #pod
34             #pod These are the arguments that will be passed to C<fill_this_in> along with the
35             #pod template, input, and a few required handlers.
36             #pod
37             #pod =cut
38              
39             has template_args => (
40             is => 'ro',
41             isa => 'HashRef',
42             );
43              
44             sub render {
45 8     8 0 83638 my ($self, $input_ref, $args)= @_;
46              
47             my $hash = $self->_enref_as_needed({
48 8 100       27 (map {; $_ => ref $args->{$_} ? $args->{$_} : \$args->{$_} } keys %$args),
  21         68  
49             });
50              
51 8         263 my $template_class = $self->template_class;
52 8         29 Module::Runtime::require_module($template_class);
53              
54             my $result = $template_class->fill_this_in(
55             $$input_ref,
56 8 50       73 %{ $self->{template_args} || {} },
57             HASH => $hash,
58 2     2   958 BROKEN => sub { my %hash = @_; die $hash{error}; },
  2         29  
59 8         4028 );
60              
61             # :-( -- rjbs, 2012-10-01
62 6 50       3035 die $Text::Template::ERROR unless defined $result;
63              
64 6         28 return \$result;
65             }
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Email::MIME::Kit::Renderer::Text::Template - render parts of your mail with Text::Template
78              
79             =head1 VERSION
80              
81             version 1.101304
82              
83             =head1 PERL VERSION
84              
85             This module should work on any version of perl still receiving updates from
86             the Perl 5 Porters. This means it should work on any version of perl released
87             in the last two to three years. (That is, if the most recently released
88             version is v5.40, then this module should work on both v5.40 and v5.38.)
89              
90             Although it may work on older versions of perl, no guarantee is made that the
91             minimum required version will not be increased. The version may be increased
92             for any reason, and there is no promise that patches will be accepted to lower
93             the minimum required perl.
94              
95             =head1 ATTRIBUTES
96              
97             =head2 template_class
98              
99             This attribute stores the name of the class that will be standing in for
100             Text::Template, if any. It defaults, obviously, to Text::Template.
101              
102             =head2 template_args
103              
104             These are the arguments that will be passed to C<fill_this_in> along with the
105             template, input, and a few required handlers.
106              
107             =head1 AUTHOR
108              
109             Ricardo Signes <cpan@semiotic.systems>
110              
111             =head1 CONTRIBUTOR
112              
113             =for stopwords Ricardo Signes
114              
115             Ricardo Signes <rjbs@semiotic.systems>
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2022 by Ricardo Signes.
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123              
124             =cut