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.037;
2             # ABSTRACT: something that renders a Text::Template template string
3              
4 16     16   12897 use Moose::Role;
  16         370  
  16         174  
5              
6 16     16   108529 use Dist::Zilla::Pragmas;
  16         43  
  16         154  
7              
8 16     16   139 use namespace::autoclean;
  16         39  
  16         172  
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   1604 use Text::Template;
  16         41  
  16         7350  
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 2775 my ($self, $string, $stash, $arg) = @_;
51              
52 65 50       247 $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         3191 %$arg,
61             );
62              
63 65 50       10865 $self->log_fatal("Could not create a Text::Template object from:\n$string")
64             unless $tmpl;
65              
66 65         489 my $content = $tmpl->fill_in(%$arg, HASH => $stash);
67              
68 65 50       77069 $self->log_fatal("Filling in the template returned undef for:\n$string")
69             unless defined $content;
70              
71 65         1093 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.037
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
99             released in the last two to three years. (That is, if the most recently
100             released version is v5.40, then this module should work on both v5.40 and
101             v5.38.)
102              
103             Although it may work on older versions of perl, no guarantee is made that the
104             minimum required version will not be increased. The version may be increased
105             for any reason, and there is no promise that patches will be accepted to
106             lower the minimum required perl.
107              
108             =head1 ATTRIBUTES
109              
110             =head2 delim
111              
112             This attribute (which can't easily be set!) is a two-element array reference
113             returning the Text::Template delimiters to use. It defaults to C<{{> and
114             C<}}>.
115              
116             =head1 METHODS
117              
118             =head2 fill_in_string
119              
120             my $rendered = $plugin->fill_in_string($template, \%stash, \%arg);
121              
122             This uses Text::Template to fill in the given template using the variables
123             given in the C<%stash>. The stash becomes the HASH argument to Text::Template,
124             so scalars must be scalar references rather than plain scalars.
125              
126             C<%arg> is dereferenced and passed in as extra arguments to Text::Template's
127             C<new> routine.
128              
129             =head1 AUTHOR
130              
131             Ricardo SIGNES 😏 <cpan@semiotic.systems>
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2026 by Ricardo SIGNES.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut