File Coverage

blib/lib/Mojolicious/Plugin/HandlebarsJSRenderer.pm
Criterion Covered Total %
statement 15 17 88.2
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 23 91.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::HandlebarsJSRenderer;
2              
3 1     1   34682 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         20  
5 1     1   4 use warnings;
  1         10  
  1         28  
6              
7 1     1   747 use Mojo::Base 'Mojolicious::Plugin';
  1         11381  
  1         6  
8 1     1   2126 use Mojo::Util qw/slurp dumper/;
  1         98418  
  1         185  
9              
10 1     1   482 use JavaScript::V8::Handlebars;
  0            
  0            
11              
12             our $VERSION = '0.04';
13              
14             has 'wrapper';
15              
16             sub register {
17             my( $self, $app, $conf ) = @_;
18             my $hbjs = JavaScript::V8::Handlebars->new;
19              
20             #TODO Clean this up
21             if( $conf->{wrapper} ) {
22             for( @{ $app->renderer->paths } ) {
23             if( -r "$_/$conf->{wrapper}" ) {
24             $self->wrapper( "$_/$conf->{wrapper}" );
25             last;
26             }
27             }
28              
29             if( $self->wrapper ) {
30             $self->wrapper( $hbjs->compile_file( $self->wrapper ) );
31             }
32             else {
33             die "Failed to find $conf->{wrapper} in @{$app->renderer->paths}";
34             }
35             }
36              
37             for( @{ $app->renderer->paths } ) {
38             next unless -d $_;
39             # Magically picks up partials as well
40             $hbjs->add_template_dir( $_ );
41             }
42              
43              
44             $app->renderer->add_handler( hbs => sub {
45             my( $r, $c, $output, $options ) = @_;
46              
47             return unless $r->template_path($options) or length $options->{inline};
48              
49             if( length $options->{inline} ) {
50             $$output = $hbjs->render_string( $options->{inline}, $c->stash );
51             }
52             elsif( my $template = $r->template_for($c) ) {
53             $$output = $hbjs->execute_template( $template, $c->stash );
54             }
55             else {
56             #TODO should this die?
57             return;
58             }
59              
60             if( $self->wrapper ) {
61             $$output = $self->wrapper->({ %{$c->stash}, content => $$output });
62             }
63              
64             return 1;
65             } );
66             }
67              
68              
69              
70             =head1 NAME
71              
72             Mojolicious::Plugin::HandlebarsJSRenderer - Render Handlebars inside Mojolicious
73              
74             =head1 SYNOPSIS
75              
76             This is a plugin for adding the Handlebars templating language to Mojolicious as a renderer.
77              
78             sub startup {
79             my $self = shift;
80             ...
81              
82             $self->plugin('HandlebarsJSRenderer', { wrapper => $wrapper });
83             $self->renderer->default_handler('hbs') #default to hbs templates instead of epl
84             ...
85             }
86              
87              
88              
89             =head1 AUTHOR
90              
91             Robert Grimes, C<< >>
92              
93             =head1 BUGS
94              
95             Please report any bugs or feature requests to C, or through
96             the web interface at L. I will be notified, and then you'll
97             automatically be notified of progress on your bug as I make changes.
98              
99              
100              
101              
102             =head1 SUPPORT
103              
104             You can find documentation for this module with the perldoc command.
105              
106             perldoc Mojolicious::Plugin::HandlebarsJSRenderer
107              
108              
109             You can also look for information at:
110              
111             =over 4
112              
113             =item * RT: CPAN's request tracker (report bugs here)
114              
115             L
116              
117             =item * AnnoCPAN: Annotated CPAN documentation
118              
119             L
120              
121             =item * CPAN Ratings
122              
123             L
124              
125             =item * Search CPAN
126              
127             L
128              
129             =back
130              
131              
132             =head1 ACKNOWLEDGEMENTS
133              
134              
135             =head1 LICENSE AND COPYRIGHT
136              
137             Copyright 2015 Robert Grimes.
138              
139             This program is free software; you can redistribute it and/or modify it
140             under the terms of the the Artistic License (2.0). You may obtain a
141             copy of the full license at:
142              
143             L
144              
145             Any use, modification, and distribution of the Standard or Modified
146             Versions is governed by this Artistic License. By using, modifying or
147             distributing the Package, you accept this license. Do not use, modify,
148             or distribute the Package, if you do not accept this license.
149              
150             If your Modified Version has been derived from a Modified Version made
151             by someone other than you, you are nevertheless required to ensure that
152             your Modified Version complies with the requirements of this license.
153              
154             This license does not grant you the right to use any trademark, service
155             mark, tradename, or logo of the Copyright Holder.
156              
157             This license includes the non-exclusive, worldwide, free-of-charge
158             patent license to make, have made, use, offer to sell, sell, import and
159             otherwise transfer the Package with respect to any patent claims
160             licensable by the Copyright Holder that are necessarily infringed by the
161             Package. If you institute patent litigation (including a cross-claim or
162             counterclaim) against any party alleging that the Package constitutes
163             direct or contributory patent infringement, then this Artistic License
164             to you shall terminate on the date that such litigation is filed.
165              
166             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
167             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
168             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
169             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
170             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
171             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
172             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
173             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
174              
175              
176             =cut
177              
178             1; # End of Mojolicious::Plugin::HandlebarsJSRenderer