File Coverage

blib/lib/Dancer2/Template/Handlebars/Helpers.pm
Criterion Covered Total %
statement 20 20 100.0
branch 1 2 50.0
condition 2 2 100.0
subroutine 6 6 100.0
pod 0 2 0.0
total 29 32 90.6


line stmt bran cond sub pod time code
1             package Dancer2::Template::Handlebars::Helpers 1.00;
2              
3             # ABSTRACT: parent class for Handlebars' helper collections
4              
5 1     1   1058 use strict;
  1         2  
  1         42  
6 1     1   6 use warnings;
  1         16  
  1         32  
7              
8 1     1   454 use Sub::Attribute;
  1         534  
  1         163  
9              
10             my %HELPERS;
11              
12             sub HANDLEBARS_HELPERS {
13 1     1 0 17 my $class = shift;
14 1 50       3 %{ $HELPERS{$class} || {} };
  1         8  
15             }
16              
17             sub Helper : ATTR_SUB {
18 2     2 0 360 my ( $class, $sym_ref, undef, undef, $attr_data ) = @_;
19              
20 2         5 my $fname = $class . '::' . *{$sym_ref}{NAME};
  2         5  
21 2   100     9 my $helper_name = $attr_data || *{$sym_ref}{NAME};
22              
23 2         18 $HELPERS{$class}{$helper_name} = \&$fname;
24 1     1   8 }
  1         2  
  1         8  
25              
26             1;
27              
28             __END__
29              
30             =pod
31              
32             =encoding UTF-8
33              
34             =head1 NAME
35              
36             Dancer2::Template::Handlebars::Helpers - parent class for Handlebars' helper collections
37              
38             =head1 VERSION
39              
40             version 1.00
41              
42             =head1 SYNOPSIS
43              
44             package MyApp::HandlebarsHelpers;
45             use parent Dancer::Template::Handlebars::Helpers;
46             sub shout :Helper {
47             my( $context, $text ) = @_;
48             return uc $text;
49             }
50             sub internal_name :Helper(whisper) {
51             my( $context, $text ) = @_;
52             return lc $text;
53             }
54             1;
55              
56             and then in the Dancer2 app config.yml:
57              
58             engines:
59             handlebars:
60             helpers:
61             - MyApp::HandlebarsHelpers
62              
63             =head1 DESCRIPTION
64              
65             Base class for modules containing Handlebars helper functions.
66             The helper functions are labelled with the C<:Helper> attribute.
67             A name for the helper function can be passed or, if not, will default
68             to the sub's name.
69              
70             Behind the curtain, what the attribute does is to add the
71             tagged functions to a module-wide C<%HANDLEBARS_HELPERS> variable,
72             which has the function names as keys and their coderefs as values.
73             For example, to register the functions of the SYNOPSIS
74             without the help of C<Dancer2::Template::Handlebars::Helpers>, one could do:
75              
76             package MyApp::HandlebarsHelpers;
77             our HANDLEBARS_HELPERS = (
78             shout => \&shout,
79             whisper => \&internal_name,
80             );
81             sub shout {
82             my( $context, $text ) = @_;
83             return uc $text;
84             }
85             sub internal_name {
86             my( $context, $text ) = @_;
87             return lc $text;
88             }
89             1;
90              
91             =head1 AUTHOR
92              
93             D Ruth Holloway <ruth@hiruthie.me>
94              
95             =head1 COPYRIGHT AND LICENSE
96              
97             This software is copyright (c) 2020 by D Ruth Holloway.
98              
99             This is free software; you can redistribute it and/or modify it under
100             the same terms as the Perl 5 programming language system itself.
101              
102             =cut