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.4;
2              
3             # ABSTRACT: Dancer2 wrapper for Handlebars templating engine
4              
5 3     3   223032 use strict;
  3         16  
  3         106  
6 3     3   17 use warnings;
  3         7  
  3         89  
7              
8 3     3   1636 use Text::Handlebars;
  3         47811  
  3         173  
9 3     3   579 use Module::Runtime qw/ use_module /;
  3         1858  
  3         29  
10              
11 3     3   729 use Moo;
  3         7792  
  3         21  
12 3     3   2390 use Try::Tiny;
  3         9  
  3         1412  
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   33 my $self = shift;
42              
43 3         7 my %helpers;
44              
45 3 100       57 if ( my $h = $self->_config->{helpers} ) {
46 1 50       29 for my $module ( ref $h ? @$h : $h ) {
47             my %h = try {
48 1     1   61 use_module($module)->HANDLEBARS_HELPERS
49             }
50             catch {
51 0     0   0 die "couldn't import helper functions from $module: $_";
52 1         14 };
53              
54 1         32 @helpers{ keys %h } = values %h;
55             }
56             }
57              
58 3         136 return \%helpers;
59             }
60              
61             sub render {
62 7     7 0 658177 my ( $self, $template, $tokens ) = @_;
63              
64 7         23 my $method = 'render';
65              
66 7 100       78 if ( ref $template ) { # it's a ref to a string
67 1         5 $template = $$template;
68 1         3 $method .= '_string';
69             }
70              
71 7         161 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 0.4
89              
90             =head1 SYNOPSIS
91              
92             in your 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 Yanick, for his prior work on L<Dancer::Template::Handlebars> and
113             L<Dancer2::Template::Mustache>. Most all of the code you see in this
114             module is his work, or very, very close to his original code; I
115             merely remixed it, and got tests working for my own purposes.
116              
117             =head1 SEE ALSO
118              
119             L<http://handlebarsjs.com>
120              
121             L<Text::Handlebars>
122              
123             =head1 SUPPORT
124              
125             You can find this documentation for this module with the perldoc command.
126              
127             perldoc Dancer2::Template::Handlebars
128              
129             You can also look for information at:
130              
131             =over 4
132              
133             =item * MetaCPAN
134              
135             L<https://metacpan.org/release/Dancer2-Template-Handlebars>
136              
137             =item * Gitlab
138              
139             L<https://gitlab.com/GeekRuthie/dancer2-template-handlebars>
140              
141             =item * Gitlab issues tracker
142              
143             L<https://gitlab.com/GeekRuthie/dancer2-template-handlebars/-/issues>
144              
145             =item * CPAN Ratings
146              
147             L<http://cpanratings.perl.org/d/Dancer2-Template-Handlebars>
148              
149             =back
150              
151             =head1 AUTHOR
152              
153             D Ruth Holloway <ruth@hiruthie.me>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2020 by D Ruth Holloway.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =cut