File Coverage

blib/lib/Dist/Zilla/Role/TextTemplate.pm
Criterion Covered Total %
statement 19 21 90.4
branch 3 6 50.0
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 28 34 82.3


line stmt bran cond sub pod time code
1             # ABSTRACT: something that renders a Text::Template template string
2              
3             use Moose::Role;
4 16     16   9250  
  16         43  
  16         133  
5             use Dist::Zilla::Pragmas;
6 16     16   81845  
  16         44  
  16         123  
7             use namespace::autoclean;
8 16     16   125  
  16         39  
  16         129  
9             #pod =head1 DESCRIPTION
10             #pod
11             #pod Plugins implementing TextTemplate may call their own C<L</fill_in_string>>
12             #pod method to render templates using L<Text::Template|Text::Template>.
13             #pod
14             #pod =cut
15              
16             use Text::Template;
17 16     16   1263  
  16         47  
  16         5584  
18             #pod =attr delim
19             #pod
20             #pod This attribute (which can't easily be set!) is a two-element array reference
21             #pod returning the Text::Template delimiters to use. It defaults to C<{{> and
22             #pod C<}}>.
23             #pod
24             #pod =cut
25              
26             # XXX: Later, add a way to set this in config. -- rjbs, 2008-06-02
27             has delim => (
28             is => 'ro',
29             isa => 'ArrayRef',
30             lazy => 1,
31             init_arg => undef,
32             default => sub { [ qw( {{ }} ) ] },
33             );
34              
35             #pod =method fill_in_string
36             #pod
37             #pod my $rendered = $plugin->fill_in_string($template, \%stash, \%arg);
38             #pod
39             #pod This uses Text::Template to fill in the given template using the variables
40             #pod given in the C<%stash>. The stash becomes the HASH argument to Text::Template,
41             #pod so scalars must be scalar references rather than plain scalars.
42             #pod
43             #pod C<%arg> is dereferenced and passed in as extra arguments to Text::Template's
44             #pod C<new> routine.
45             #pod
46             #pod =cut
47              
48             my ($self, $string, $stash, $arg) = @_;
49              
50 64     64 1 2327 $self->log_fatal("Cannot use undef as a template string")
51             unless defined $string;
52 64 50       258  
53             my $tmpl = Text::Template->new(
54             TYPE => 'STRING',
55             SOURCE => $string,
56             DELIMITERS => $self->delim,
57             BROKEN => sub { my %hash = @_; die $hash{error}; },
58             %$arg,
59 0     0   0 );
  0         0  
60 64         2320  
61             $self->log_fatal("Could not create a Text::Template object from:\n$string")
62             unless $tmpl;
63 64 50       9670  
64             my $content = $tmpl->fill_in(%$arg, HASH => $stash);
65              
66 64         466 $self->log_fatal("Filling in the template returned undef for:\n$string")
67             unless defined $content;
68 64 50       59989  
69             return $content;
70             }
71 64         867  
72             1;
73              
74              
75             =pod
76              
77             =encoding UTF-8
78              
79             =head1 NAME
80              
81             Dist::Zilla::Role::TextTemplate - something that renders a Text::Template template string
82              
83             =head1 VERSION
84              
85             version 6.028
86              
87             =head1 DESCRIPTION
88              
89             Plugins implementing TextTemplate may call their own C<L</fill_in_string>>
90             method to render templates using L<Text::Template|Text::Template>.
91              
92             =head1 PERL VERSION
93              
94             This module should work on any version of perl still receiving updates from
95             the Perl 5 Porters. This means it should work on any version of perl released
96             in the last two to three years. (That is, if the most recently released
97             version is v5.40, then this module should work on both v5.40 and v5.38.)
98              
99             Although it may work on older versions of perl, no guarantee is made that the
100             minimum required version will not be increased. The version may be increased
101             for any reason, and there is no promise that patches will be accepted to lower
102             the minimum required perl.
103              
104             =head1 ATTRIBUTES
105              
106             =head2 delim
107              
108             This attribute (which can't easily be set!) is a two-element array reference
109             returning the Text::Template delimiters to use. It defaults to C<{{> and
110             C<}}>.
111              
112             =head1 METHODS
113              
114             =head2 fill_in_string
115              
116             my $rendered = $plugin->fill_in_string($template, \%stash, \%arg);
117              
118             This uses Text::Template to fill in the given template using the variables
119             given in the C<%stash>. The stash becomes the HASH argument to Text::Template,
120             so scalars must be scalar references rather than plain scalars.
121              
122             C<%arg> is dereferenced and passed in as extra arguments to Text::Template's
123             C<new> routine.
124              
125             =head1 AUTHOR
126              
127             Ricardo SIGNES 😏 <cpan@semiotic.systems>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is copyright (c) 2022 by Ricardo SIGNES.
132              
133             This is free software; you can redistribute it and/or modify it under
134             the same terms as the Perl 5 programming language system itself.
135              
136             =cut