File Coverage

blib/lib/Kelp/Template.pm
Criterion Covered Total %
statement 26 27 96.3
branch 9 10 90.0
condition 6 6 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 47 49 95.9


line stmt bran cond sub pod time code
1              
2             use Kelp::Base;
3 30     30   560 use Template::Tiny;
  30         48  
  30         152  
4 30     30   13752 use Path::Tiny;
  30         33876  
  30         943  
5 30     30   843  
  30         11953  
  30         10327  
6             attr paths => sub { [] };
7             attr encoding => 'utf8';
8             attr tt => sub { Template::Tiny->new };
9              
10             my ( $self, $template, $vars ) = @_;
11              
12 44     44 1 282 my $ref = ref $template;
13              
14 44         69 # A GLOB or an IO object will be read and returned as a SCALAR template
15             # No reference means a file name
16             if ( $ref =~ /^IO/ || $ref eq 'GLOB' || !$ref ) {
17             if ( !$ref ) {
18 44 100 100     307 for my $p ( '.', @{ $self->paths } ) {
    50 100        
19 38 100       84 if ( -e ( my $fullpath = "$p/$template" ) ) {
20 36         46 $template = $fullpath;
  36         115  
21 69 100       1182 last;
22 21         56 }
23 21         42 }
24             }
25             $template = $self->_read_file($template);
26             }
27 38         132 elsif ( $ref ne 'SCALAR' ) {
28             die "Template reference must be SCALAR, GLOB or an IO object";
29             }
30 0         0  
31             my $output;
32             $self->tt->process( $template, $vars, \$output );
33 29         54 return $output;
34 29         91 }
35 29         6255  
36             my ( $self, $file ) = @_;
37              
38             local $/ = undef;
39 38     38   101 my $text =
40             ref $file
41 38         159 ? <$file>
42 38 100       214 : path($file)->slurp({ binmode => ':encoding(' . $self->encoding . ')' })
43             ;
44              
45             return \$text;
46             }
47              
48 23         24811 1;
49              
50              
51             =pod
52              
53             =head1 NAME
54              
55             Kelp::Template - A very minimal template rendering engine for Kelp
56              
57             =head1 SYNOPSIS
58              
59             my $t = Kelp::Template->new;
60             say $t->process('file.tt', { bar => 'foo' });
61              
62             =head1 DESCRIPTION
63              
64             This module provides basic template rendering using L<Template::Tiny>.
65              
66             =head1 ATTRIBUTES
67              
68             =head2 paths
69              
70             An arrayref of paths to use when looking for template files.
71              
72             =head2 encoding
73              
74             Specifies the text encoding of the template files. The default value is C<utf8>.
75              
76             =head1 METHODS
77              
78             =head2 process( $template, \%vars )
79              
80             Processes a template and returns the parsed text. The template may be a file name,
81             a reference to a text, a GLOB or an IO object.
82              
83             say $t->process(\"Hello [% who %]", { who => 'you' });
84              
85             =cut