File Coverage

blib/lib/Dancer/Template/Handlebars.pm
Criterion Covered Total %
statement 47 51 92.1
branch 10 14 71.4
condition 2 3 66.6
subroutine 13 14 92.8
pod 5 6 83.3
total 77 88 87.5


line stmt bran cond sub pod time code
1             package Dancer::Template::Handlebars;
2             BEGIN {
3 2     2   5208 $Dancer::Template::Handlebars::AUTHORITY = 'cpan:YANICK';
4             }
5             # ABSTRACT: Wrapper for the Handlebars template system
6             $Dancer::Template::Handlebars::VERSION = '0.2.1';
7              
8 2     2   17 use strict;
  2         4  
  2         58  
9 2     2   10 use warnings;
  2         4  
  2         52  
10              
11 2     2   12 use Dancer::Config 'setting';
  2         3  
  2         129  
12              
13 2     2   1807 use Text::Handlebars;
  2         29972  
  2         111  
14              
15 2     2   2133 use Moo;
  2         39979  
  2         15  
16             extends 'Dancer::Template::Abstract';
17              
18             has views_root => (
19             is => 'ro',
20             lazy => 1,
21             default => sub {
22             Dancer::App->current->setting('views')
23             },
24             );
25              
26             has helpers => (
27             is => 'ro',
28             lazy => 1,
29             builder => '_build_helpers',
30             );
31              
32             sub _build_helpers {
33 2     2   1064 my $self = shift;
34            
35 2         4 my %helpers;
36              
37 2 50       10 if ( my $h = $self->config->{helpers} ) {
38 2 50       31 for my $module ( ref $h ? @$h : $h ) {
39 2     2   1071 my %h = eval "use $module; %".$module.'::HANDLEBARS_HELPERS';
  2         76  
  2         48  
  2         176  
40              
41 2 50       11 die "couldn't import helper functions from $module: $@" if $@;
42              
43 2         15 @helpers{ keys %h } = values %h;
44             }
45             }
46              
47 2         83 return \%helpers;
48             }
49              
50             has _engine => (
51             is => 'ro',
52             lazy => 1,
53             default => sub {
54             my $self = shift;
55              
56             return Text::Handlebars->new(
57             path => [
58             $self->views_root
59             ],
60             %{ $self->config },
61             helpers => $self->helpers,
62             );
63             },
64             );
65              
66             sub gather_helpers {
67 0     0 0 0 my( $self, $modules ) = @_;
68              
69             }
70              
71              
72 9     9 1 153 sub default_tmpl_ext { "hbs" }
73              
74             sub view {
75 10     10 1 149852 my ($self, $view) = @_;
76              
77 10 100       47 return $view if ref $view;
78              
79 8         45 for my $view_path ($self->_template_name($view)) {
80 16 100       737 return $view_path if -f join '/', $self->views_root, $view_path;
81             }
82              
83             # No matching view path was found
84 0         0 return;
85             }
86              
87             sub layout {
88 1     1 1 1566 my ($self, $layout, $tokens, $content) = @_;
89              
90 1         7 my $dir = Dancer::App->current->setting('views');
91 2         37 my( $layout_name ) = grep { -e join '/', $dir, $_ }
  2         17  
92 1         26 map { 'layouts/'.$_ } $self->_template_name($layout);
93              
94 1         3 my $full_content;
95 1 50       17 if (-e join '/', $dir, $layout_name ) {
96 1         11 $full_content = Dancer::Template->engine->render(
97             $layout_name, {%$tokens, content => $content});
98             } else {
99 0         0 $full_content = $content;
100 0         0 Dancer::Logger::error("Defined layout ($layout) was not found!");
101             }
102 1         389 $full_content;
103             }
104              
105             sub view_exists {
106 5     5 1 162 my( $self, $template) = @_;
107              
108             # string ref or file
109 5   66     122 return ref($template) || -f join '/', $self->views_root, $template;
110              
111             }
112              
113             sub render {
114 6     6 1 666 my ($self, $template, $tokens) = @_;
115              
116 6 100       27 if ( ref $template ) {
117 1         7 return $self->_engine->render_string( $$template, $tokens );
118             }
119              
120 5         125 return $self->_engine->render( $template, $tokens );
121              
122             }
123              
124             1;
125              
126             __END__