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 1.00;
2              
3             # ABSTRACT: Dancer2 wrapper for Handlebars templating engine
4              
5 3     3   221675 use strict;
  3         17  
  3         101  
6 3     3   17 use warnings;
  3         5  
  3         106  
7              
8 3     3   1511 use Text::Handlebars;
  3         46827  
  3         168  
9 3     3   677 use Module::Runtime qw/ use_module /;
  3         1817  
  3         25  
10              
11 3     3   762 use Moo;
  3         8227  
  3         21  
12 3     3   2381 use Try::Tiny;
  3         7  
  3         1281  
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   73 my $self = shift;
42              
43 3         6 my %helpers;
44              
45 3 100       60 if ( my $h = $self->_config->{helpers} ) {
46 1 50       26 for my $module ( ref $h ? @$h : $h ) {
47             my %h = try {
48 1     1   53 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         29 @helpers{ keys %h } = values %h;
55             }
56             }
57              
58 3         135 return \%helpers;
59             }
60              
61             sub render {
62 7     7 0 597440 my ( $self, $template, $tokens ) = @_;
63              
64 7         20 my $method = 'render';
65              
66 7 100       64 if ( ref $template ) { # it's a ref to a string
67 1         3 $template = $$template;
68 1         4 $method .= '_string';
69             }
70              
71 7         141 return $self->_engine->$method( $template, $tokens );
72             }
73              
74             1;
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =head1 NAME
83              
84             Dancer2::Template::Handlebars - Dancer2 wrapper for Handlebars templating engine
85              
86             =head1 VERSION
87              
88             version 1.00
89              
90             =head1 SYNOPSIS
91              
92             in your L<Dancer2> app config.yml:
93              
94             template: "handlebars"
95              
96             ...and if you want to add custom helpers:
97              
98             engines:
99             handlebars:
100             helpers:
101             - MyApp::HandlebarsHelpers
102              
103             =head1 HELPERS
104              
105             You can create custom modules full of helpers to use in your Handlebars templates. For
106             more details on creating these, see L<Dancer2::Template::Handlebars::Helpers>.
107              
108             Handlebars comes with helpers C<with>, C<each>, C<if>, and C<unless>.
109              
110             =head1 GRATEFUL THANKS
111              
112             ...to L<Yanick|https://metacpan.org/author/YANICK>, for his prior work on
113             L<Dancer::Template::Handlebars> and L<Dancer2::Template::Mustache>.
114             Most all of the code you see in this module is his work, or very, very
115             close to his original code; I merely remixed it, and got tests working
116             for my own purposes.
117              
118             =head1 SEE ALSO
119              
120             L<http://handlebarsjs.com>
121              
122             L<Text::Handlebars>
123              
124             =head1 SUPPORT
125              
126             You can find this documentation for this module with the perldoc command.
127              
128             perldoc Dancer2::Template::Handlebars
129              
130             You can also look for information at:
131              
132             =over 4
133              
134             =item * MetaCPAN
135              
136             L<https://metacpan.org/release/Dancer2-Template-Handlebars>
137              
138             =item * Gitlab
139              
140             L<https://gitlab.com/GeekRuthie/dancer2-template-handlebars>
141              
142             =item * Gitlab issues tracker
143              
144             L<https://gitlab.com/GeekRuthie/dancer2-template-handlebars/-/issues>
145              
146             =item * CPAN Ratings
147              
148             L<http://cpanratings.perl.org/d/Dancer2-Template-Handlebars>
149              
150             =back
151              
152             =head1 AUTHOR
153              
154             D Ruth Holloway <ruth@hiruthie.me>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2020 by D Ruth Holloway.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut