| 1 | package Kelp::Template; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 39 | ▶ | use Kelp::Base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 39 | ▶ | use Template::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 39 | ▶ | use Path::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 39 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 0 | ▶ | attr paths => sub { [] }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 9 | 39 | attr encoding => 'UTF-8'; | |||||||||||||||||||
| 10 | 18 | ▶ | attr tt => sub { Template::Tiny->new }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub process | ||||||||||||||||||||
| 13 | 94 | ▶ | { | ||||||||||||||||||
|
Subroutine
process: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 14 | 94 | my ($self, $template, $vars) = @_; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 94 | my $ref = ref $template; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # A GLOB or an IO object will be read and returned as a SCALAR template | ||||||||||||||||||||
| 19 | # No reference means a file name | ||||||||||||||||||||
| 20 | 94 | ▶ | if (!$ref) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 21 | 73 | $template = $self->_read_file($self->find_template($template)); | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | ▶ | elsif ($ref =~ /^IO/ || $ref eq 'GLOB') { | |||||||||||||||||||
|
Condition100%3/3
$ref =~ /^IO/ or $ref eq "GLOB"
MC/DC100%2/2
$ref =~ /^IO/ or $ref eq "GLOB"
$ref =~ /^IO/
$ref eq "GLOB"
Truth table100%3/3
A or B
A= $ref =~ /^IO/B= $ref eq "GLOB"
| |||||||||||||||||||||
| 24 | 5 | $template = $self->_read_file($template); | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | elsif ($ref ne 'SCALAR') { | ||||||||||||||||||||
| 27 | 0 | croak "Template reference must be SCALAR, GLOB or an IO object"; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 70 | my $output; | |||||||||||||||||||
| 31 | 70 | $self->tt->process($template, $vars, \$output); | |||||||||||||||||||
| 32 | 70 | return $output; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | sub find_template | ||||||||||||||||||||
| 36 | 73 | ▶ | { | ||||||||||||||||||
|
Subroutine
find_template: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 37 | 73 | my ($self, $name) = @_; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 73 | my $file; | |||||||||||||||||||
| 40 | 73 | for my $p ('.', @{$self->paths}) { | |||||||||||||||||||
| 41 | 143 | $file = "$p/$name"; | |||||||||||||||||||
| 42 | 143 | ▶ | return $file if -e $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 24 | return undef; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub _read_file | ||||||||||||||||||||
| 49 | 78 | ▶ | { | ||||||||||||||||||
|
Subroutine
_read_file: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 50 | 78 | my ($self, $file) = @_; | |||||||||||||||||||
| 51 | 78 | my $text; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 78 | ▶ | if (ref $file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # read the entire file | ||||||||||||||||||||
| 56 | 5 | local $/ = undef; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # make sure to properly rewind the handle after we read from it | ||||||||||||||||||||
| 59 | 5 | my $pos = tell $file; | |||||||||||||||||||
| 60 | 5 | $text = readline $file; | |||||||||||||||||||
| 61 | 5 | seek $file, $pos, 0; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||
| 64 | 73 | $text = path($file)->slurp( | |||||||||||||||||||
| 65 | {binmode => ':encoding(' . $self->encoding . ')'} | ||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 54 | return \$text; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 39 | 1; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | __END__ | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =pod | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 NAME | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Kelp::Template - A very minimal template rendering engine for Kelp | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head1 SYNOPSIS | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | my $t = Kelp::Template->new; | ||||||||||||||||||||
| 85 | say $t->process('file.tt', { bar => 'foo' }); | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head1 DESCRIPTION | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | This module provides basic template rendering using L<Template::Tiny>. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head2 paths | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | An arrayref of paths to use when looking for template files. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head2 encoding | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | Specifies the text encoding of the template files. The default value is C<UTF-8>. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head1 METHODS | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head2 process( $template, \%vars ) | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | Processes a template and returns the parsed text. The template may be a file name, | ||||||||||||||||||||
| 106 | a reference to a text, a GLOB or an IO object. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | say $t->process(\"Hello [% who %]", { who => 'you' }); | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||
| 111 | |||||||||||||||||||||