Handlebars.pm

filter: Statementstmt 93.9% 62 / 66 Branchbran 75.0% 9 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 16 / 17 Podpod 100% 5 / 5 totaltotal 89.5% 94 / 105 CC 10 SCAR 8.0
CC
10
Cov
90%
_build_helpers
4.2
view
4.0
layout
2.0
CRAP 2.2
↑ Summary Handlebars/Helpers.pm »
1 package Dancer::Template::Handlebars;
2 3 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Wrapper for the Handlebars template system
4 3 $Dancer::Template::Handlebars::VERSION = '0.2.2';
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Text::Handlebars;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Module::Runtime qw/ use_module /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Try::Tiny;
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 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 Dancer::App->current->setting('views')
23 },
24 3 );
25  
26 3 has helpers => (
27 is => 'ro',
28 lazy => 1,
29 builder => '_build_helpers',
30 );
31  
32 2 ▶sub _build_helpers {
Subroutine _build_helpers: called CC=4 SCAR=14.3
33 2 my $self = shift;
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 = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 use_module( $module )->HANDLEBARS_HELPERS
41 }
42 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 0 die "couldn't import helper functions from $module: $_";
44 2 };
45  
46 2 @helpers{ keys %h } = values %h;
47 }
48 }
49  
50 2 return \%helpers;
51 }
52  
53 has _engine => (
54 is => 'ro',
55 lazy => 1,
56 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 2 my $self = shift;
58  
59 return Text::Handlebars->new(
60 path => [
61 $self->views_root
62 ],
63 2 %{ $self->config },
64 helpers => $self->helpers,
65 );
66 },
67 3 );
68  
69 9 ▶sub default_tmpl_ext { "hbs" }
Subroutine default_tmpl_ext: called CC=1 SCAR=0.0
Pod documented
70  
71 10 ▶sub view {
Subroutine view: called CC=4 SCAR=13.9
Pod documented
72 10 my ($self, $view) = @_;
73  
74 10 ▶ return $view if ref $view;
Branch: 1 branch
BranchTrueFalseTotal
if ref $view 2 8 10
75  
76 8 for my $view_path ($self->_template_name($view)) {
77 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
78 }
79  
80 # No matching view path was found
81 0 return;
82 }
83  
84 1 ▶sub layout {
Subroutine layout: called CC=2 SCAR=7.2
Pod documented
85 1 my ($self, $layout, $tokens, $content) = @_;
86  
87 1 my $dir = Dancer::App->current->setting('views');
88 2 my( $layout_name ) = grep { -e join '/', $dir, $_ }
89 1 map { 'layouts/'.$_ } $self->_template_name($layout);
90  
91 1 my $full_content;
92 1 ▶ if (-e join '/', $dir, $layout_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e join('/', $dir, $layout_name)) { } 1 0 1
93 1 $full_content = Dancer::Template->engine->render(
94 $layout_name, {%$tokens, content => $content});
95 } else {
96 0 $full_content = $content;
97 0 Dancer::Logger::error("Defined layout ($layout) was not found!");
98 }
99 1 $full_content;
100 }
101  
102 5 ▶sub view_exists {
Subroutine view_exists: called CC=2 SCAR=7.1
Pod documented
103 5 my( $self, $template) = @_;
104  
105 # string ref or file
106 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
107  
108 }
109  
110 6 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod documented
111 6 my ($self, $template, $tokens) = @_;
112  
113 6 my $method = 'render';
114  
115 6 ▶ if ( ref $template ) { # it's a ref to a string
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) 1 5 6
116 1 $template = $$template;
117 1 $method .= '_string';
118 }
119  
120 6 return $self->_engine->$method( $template, $tokens );
121  
122 }
123  
124 3 1;
125  
126 __END__
↑ Summary Handlebars/Helpers.pm »