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