blib/lib/Dancer/Template/Handlebars.pm

filter: Statementstmt 93.4% 57 / 61 Branchbran 71.4% 10 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.7% 15 / 16 Podpod 83.3% 5 / 6 totaltotal 87.2% 89 / 102 SCAR 24.1
CC
11
Cov
88%
_build_helpers
5.1
view
4.0
layout
2.0
CRAP 11.2
↑ Summary Build »
1 package Dancer::Template::Handlebars;
2 BEGIN {
3 3 ▶ $Dancer::Template::Handlebars::AUTHORITY = 'cpan:YANICK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 # ABSTRACT: Wrapper for the Handlebars template system
6 3 $Dancer::Template::Handlebars::VERSION = '0.2.1';
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Text::Handlebars;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 extends 'Dancer::Template::Abstract';
17  
18 has views_root => (
19 is => 'ro',
20 lazy => 1,
21 default => sub {
22 2 ▶ Dancer::App->current->setting('views')
Subroutine __ANON__: called CC=1 SCAR=0.0
23 },
24 3 );
25  
26 3 has helpers => (
27 is => 'ro',
28 lazy => 1,
29 builder => '_build_helpers',
30 );
31  
32 sub _build_helpers {
33 2 ▶ my $self = shift;
Subroutine _build_helpers: called CC=5 SCAR=16.4
34
35 2 my %helpers;
36  
37 2 ▶ if ( my $h = $self->config->{helpers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $self->config->{"helpers"}) 2 0 2
38 2 ▶ for my $module ( ref $h ? @$h : $h ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $h ? : 2 0 2
39 2 ▶ my %h = eval "use $module; %".$module.'::HANDLEBARS_HELPERS';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 ▶ die "couldn't import helper functions from $module: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
42  
43 2 @helpers{ keys %h } = values %h;
44 }
45 }
46  
47 2 return \%helpers;
48 }
49  
50 has _engine => (
51 is => 'ro',
52 lazy => 1,
53 default => sub {
54 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 return Text::Handlebars->new(
57 path => [
58 $self->views_root
59 ],
60 2 %{ $self->config },
61 helpers => $self->helpers,
62 );
63 },
64 3 );
65  
66 sub gather_helpers {
67 0 ▶ my( $self, $modules ) = @_;
Subroutine gather_helpers: not called CC=1 SCAR=6.9
Pod undocumented
68  
69 }
70  
71  
72 9 ▶sub default_tmpl_ext { "hbs" }
Subroutine default_tmpl_ext: called CC=1 SCAR=0.0
Pod documented
73  
74 sub view {
75 10 ▶ my ($self, $view) = @_;
Subroutine view: called CC=4 SCAR=13.9
Pod documented
76  
77 10 ▶ return $view if ref $view;
Branch: 1 branch
BranchTrueFalseTotal
if ref $view 2 8 10
78  
79 8 for my $view_path ($self->_template_name($view)) {
80 16 ▶ return $view_path if -f join '/', $self->views_root, $view_path;
Branch: 1 branch
BranchTrueFalseTotal
if -f join("/", $self->views_root, $view_path) 8 8 16
81 }
82  
83 # No matching view path was found
84 0 return;
85 }
86  
87 sub layout {
88 1 ▶ my ($self, $layout, $tokens, $content) = @_;
Subroutine layout: called CC=2 SCAR=7.2
Pod documented
89  
90 1 my $dir = Dancer::App->current->setting('views');
91 2 my( $layout_name ) = grep { -e join '/', $dir, $_ }
92 1 map { 'layouts/'.$_ } $self->_template_name($layout);
93  
94 1 my $full_content;
95 1 ▶ if (-e join '/', $dir, $layout_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e join('/', $dir, $layout_name)) { } 1 0 1
96 1 $full_content = Dancer::Template->engine->render(
97 $layout_name, {%$tokens, content => $content});
98 } else {
99 0 $full_content = $content;
100 0 Dancer::Logger::error("Defined layout ($layout) was not found!");
101 }
102 1 $full_content;
103 }
104  
105 sub view_exists {
106 5 ▶ my( $self, $template) = @_;
Subroutine view_exists: called CC=2 SCAR=7.1
Pod documented
107  
108 # string ref or file
109 5 ▶ return ref($template) || -f join '/', $self->views_root, $template;
Condition66%2/3
ref $template || -f join('/', $self->views_root, $template)
l!l&&r!l&&!r
140
MC/DC0%0/2
ref $template || -f join('/', $self->views_root, $template)
ref $template -f join('/', $self->views_root, $template)
Truth table66%2/3
A || B
A= ref $templateB= -f join('/', $self->views_root, $template)
ABresult
1X1
011
000
110  
111 }
112  
113 sub render {
114 6 ▶ my ($self, $template, $tokens) = @_;
Subroutine render: called CC=2 SCAR=6.9
Pod documented
115  
116 6 ▶ if ( ref $template ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) 1 5 6
117 1 return $self->_engine->render_string( $$template, $tokens );
118 }
119  
120 5 return $self->_engine->render( $template, $tokens );
121  
122 }
123  
124 3 1;
125  
126 __END__
↑ Summary Build »