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