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   44531 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         1  
  1         26  
5 1     1   5 use warnings;
  1         5  
  1         29  
6              
7 1     1   849 use Mojo::Base 'Mojolicious::Plugin';
  1         22003  
  1         11  
8 1     1   3888 use Mojo::Util qw/slurp dumper/;
  1         228966  
  1         218  
9              
10 1     1   530 use JavaScript::V8::Handlebars;
  0            
  0            
11              
12             our $VERSION = '0.06';
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/read $conf->{wrapper} in @{$app->renderer->paths}";
34             }
35             }
36              
37             if( $conf->{helpers} ) {
38              
39             my $helper_path;
40             for( @{ $app->renderer->paths } ) {
41             if( -r "$_/$conf->{helpers}" ) {
42             $helper_path = "$_/$conf->{helpers}";
43             last;
44             }
45             }
46              
47             die unless $helper_path;
48              
49             $hbjs->eval_file($helper_path);
50             }
51              
52             for( @{ $app->renderer->paths } ) {
53             next unless -d $_;
54             # Magically picks up partials as well
55             $hbjs->add_template_dir( $_ );
56             }
57              
58              
59             $app->renderer->add_handler( hbs => sub {
60             my( $r, $c, $output, $options ) = @_;
61              
62             return unless $r->template_path($options) or length $options->{inline};
63              
64             if( length $options->{inline} ) {
65             $$output = $hbjs->render_string( $options->{inline}, $c->stash );
66             }
67             elsif( $options->{template} ) {
68             $$output = $hbjs->execute_template( $options->{template}, $c->stash );
69             }
70             elsif( my $template = $r->template_for($c) ) {
71             $$output = $hbjs->execute_template( $template, $c->stash );
72             }
73             else {
74             #TODO should this die?
75             return;
76             }
77              
78             if( $self->wrapper ) {
79             $$output = $self->wrapper->({ %{$c->stash}, content => $$output });
80             }
81              
82             return 1;
83             } );
84             }
85              
86              
87              
88             =head1 NAME
89              
90             Mojolicious::Plugin::HandlebarsJSRenderer - Render Handlebars inside Mojolicious
91              
92             =head1 SYNOPSIS
93              
94             This is a plugin for adding the Handlebars templating language to Mojolicious as a renderer.
95              
96             sub startup {
97             my $self = shift;
98             ...
99              
100             $self->plugin('HandlebarsJSRenderer', { [wrapper => "wrapper.hbs"], [helpers => "helpers.js"] });
101             $self->renderer->default_handler('hbs') #default to hbs templates instead of epl
102             ...
103             }
104              
105             Note that by default when this plugin is initialized it attempts to cache every .hbs file inside your templates directory, which includes registering any files under a directory named 'partials' as a partial with the same name as the file. You can also pass a wrapper file which is executed after any render call and passed the results of the first render as the variable 'content'.
106              
107             Specifying a helpers.js file allow you to execute Handlebars.registerHelper() statements to provide functions to your templates. Or really, any other code you want. Any JS in this file is executed in the same global context that contains the Handlebars object that containers registeredHelpers, stored templates and is invokved to compile and execute new templates.
108              
109             Automatically found parsers are stored as 'partial/partialname'; for example: ./templates/partials/things/myfoo.hbs; may be accessed as C<< {{>partials/things/myfoo}} >>
110              
111             =head1 AUTHOR
112              
113             Robert Grimes, C<< >>
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to C, or through
118             the web interface at L. I will be notified, and then you'll
119             automatically be notified of progress on your bug as I make changes.
120              
121              
122              
123              
124             =head1 SUPPORT
125              
126             You can find documentation for this module with the perldoc command.
127              
128             perldoc Mojolicious::Plugin::HandlebarsJSRenderer
129              
130              
131             You can also look for information at:
132              
133             =over 4
134              
135             =item * RT: CPAN's request tracker (report bugs here)
136              
137             L
138              
139             =item * AnnoCPAN: Annotated CPAN documentation
140              
141             L
142              
143             =item * CPAN Ratings
144              
145             L
146              
147             =item * Search CPAN
148              
149             L
150              
151             =back
152              
153              
154             =head1 ACKNOWLEDGEMENTS
155              
156              
157             =head1 LICENSE AND COPYRIGHT
158              
159             Copyright 2015 Robert Grimes.
160              
161             This program is free software; you can redistribute it and/or modify it
162             under the terms of the the Artistic License (2.0). You may obtain a
163             copy of the full license at:
164              
165             L
166              
167             Any use, modification, and distribution of the Standard or Modified
168             Versions is governed by this Artistic License. By using, modifying or
169             distributing the Package, you accept this license. Do not use, modify,
170             or distribute the Package, if you do not accept this license.
171              
172             If your Modified Version has been derived from a Modified Version made
173             by someone other than you, you are nevertheless required to ensure that
174             your Modified Version complies with the requirements of this license.
175              
176             This license does not grant you the right to use any trademark, service
177             mark, tradename, or logo of the Copyright Holder.
178              
179             This license includes the non-exclusive, worldwide, free-of-charge
180             patent license to make, have made, use, offer to sell, sell, import and
181             otherwise transfer the Package with respect to any patent claims
182             licensable by the Copyright Holder that are necessarily infringed by the
183             Package. If you institute patent litigation (including a cross-claim or
184             counterclaim) against any party alleging that the Package constitutes
185             direct or contributory patent infringement, then this Artistic License
186             to you shall terminate on the date that such litigation is filed.
187              
188             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
189             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
190             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
191             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
192             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
193             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
194             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
195             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
196              
197              
198             =cut
199              
200             1; # End of Mojolicious::Plugin::HandlebarsJSRenderer