File Coverage

blib/lib/Dancer2/Template/HTCompiled.pm
Criterion Covered Total %
statement 35 35 100.0
branch 6 6 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 49 49 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Template::HTCompiled;
2 2     2   54510 use strict;
  2         3  
  2         82  
3 2     2   12 use warnings;
  2         4  
  2         73  
4              
5 2     2   327892 use Moo;
  2         275605  
  2         17  
6 2     2   23069 use Carp qw/ croak /;
  2         6  
  2         162  
7 2     2   7093 use HTML::Template::Compiled;
  2         839220  
  2         25  
8              
9             with 'Dancer2::Core::Role::Template';
10              
11             has '+default_tmpl_ext' => (
12             default => sub { 'html' }
13             );
14             has '+engine' => (
15             isa => sub {
16             $_[0] eq "HTML::Template::Compiled"
17             },
18             );
19              
20             sub _build_engine {
21 1     1   872 'HTML::Template::Compiled'
22             }
23             # my ($self) = @_;
24             #
25             # my %config = %{ $self->config };
26             #
27             # # Dancer2 inject a couple options without asking; Text::Xslate protests:
28             # delete $config{environment};
29             # if ( my $location = delete $config{location} ) {
30             # $config{path} //= [$location];
31             # }
32             #
33             # return Text::Xslate->new(%config);
34             #}
35              
36             sub render {
37 4     4 1 2576 my ($self, $tmpl, $vars) = @_;
38              
39 4         6 my %config = %{ $self->config };
  4         17  
40 4         844 my $env = delete $config{environment};
41 4         12 my $location = delete $config{location};
42 4         9 my $path = delete $config{path};
43 4 100       20 $path = "$location/$path" if defined $location;
44 4         9 $config{path} = $path;
45 4         78 my $htc = $self->engine;
46 4         36 my $content = eval {
47 4         5 my $t;
48 4 100       16 if ( ref($tmpl) eq 'SCALAR' ) {
49 2         11 $t = $htc->new_scalar_ref($tmpl, %config);
50             }
51             else {
52 2         23 $t = $htc->new_file($tmpl, %config);
53             }
54 3         6277 $t->param($vars);
55 3         71 $t->output;
56             };
57              
58 4 100       1425 if (my $error = $@) {
59 1         30 croak $error;
60             }
61              
62 3         18 return $content;
63             }
64              
65             1;
66              
67             __END__