File Coverage

blib/lib/Dancer2/Template/Handlebars.pm
Criterion Covered Total %
statement 32 33 96.9
branch 5 6 83.3
condition n/a
subroutine 9 10 90.0
pod 0 1 0.0
total 46 50 92.0


line stmt bran cond sub pod time code
1             package Dancer2::Template::Handlebars 0.3;
2              
3             # ABSTRACT: Dancer2 wrapper for Handlebars templating engine
4              
5 3     3   201849 use strict;
  3         15  
  3         97  
6 3     3   17 use warnings;
  3         7  
  3         82  
7              
8 3     3   1548 use Text::Handlebars;
  3         45619  
  3         152  
9 3     3   519 use Module::Runtime qw/ use_module /;
  3         1745  
  3         24  
10              
11 3     3   667 use Moo;
  3         8444  
  3         21  
12 3     3   2132 use Try::Tiny;
  3         10  
  3         1280  
13             with 'Dancer2::Core::Role::Template';
14              
15             has '+default_tmpl_ext' => ( default => sub { 'hbs' }, );
16              
17             has helpers => (
18             is => 'ro',
19             lazy => 1,
20             builder => '_build_helpers',
21             );
22              
23             has _engine => (
24             is => 'ro',
25             lazy => 1,
26             default => sub {
27             my $self = shift;
28             return Text::Handlebars->new( helpers => $self->helpers, );
29             },
30             );
31              
32             has _config => (
33             is => 'ro',
34             lazy => 1,
35             default => sub {
36             return $_[0]->settings->{engines}->{handlebars} || {};
37             }
38             );
39              
40             sub _build_helpers {
41 3     3   37 my $self = shift;
42              
43 3         8 my %helpers;
44              
45 3 100       55 if ( my $h = $self->_config->{helpers} ) {
46 1 50       30 for my $module ( ref $h ? @$h : $h ) {
47             my %h = try {
48 1     1   58 use_module($module)->HANDLEBARS_HELPERS
49             }
50             catch {
51 0     0   0 die "couldn't import helper functions from $module: $_";
52 1         11 };
53              
54 1         30 @helpers{ keys %h } = values %h;
55             }
56             }
57              
58 3         129 return \%helpers;
59             }
60              
61             sub render {
62 7     7 0 595903 my ( $self, $template, $tokens ) = @_;
63              
64 7         20 my $method = 'render';
65              
66 7 100       31 if ( ref $template ) { # it's a ref to a string
67 1         3 $template = $$template;
68 1         4 $method .= '_string';
69             }
70              
71 7         144 return $self->_engine->$method( $template, $tokens );
72             }
73              
74             1;
75              
76             __END__