File Coverage

blib/lib/Mojolicious/Plugin/ProRenderer.pm
Criterion Covered Total %
statement 15 53 28.3
branch 0 12 0.0
condition 0 7 0.0
subroutine 5 10 50.0
pod 2 2 100.0
total 22 84 26.1


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ProRenderer;
2             {
3             $Mojolicious::Plugin::ProRenderer::VERSION = '0.40';
4             }
5              
6 1     1   20762 use strict;
  1         2  
  1         30  
7 1     1   4 use warnings;
  1         2  
  1         23  
8 1     1   15 use v5.10;
  1         7  
  1         43  
9              
10 1     1   8 use base 'Mojolicious::Plugin';
  1         2  
  1         661  
11 1     1   11274 use HTML::Template::Pro ();
  1         6269  
  1         540  
12              
13             __PACKAGE__->attr('pro');
14              
15             # register plugin
16             sub register {
17 0     0 1   my ($self, $app, $args) = @_;
18              
19 0   0       $args ||= {};
20              
21             # $args{template_options}{path} = [ './templates']
22 0 0         $args->{template_options}{path} = $app->{renderer}{paths}
23             unless exists $args->{template_options}{path};
24              
25 0           my $template = $self->build(%$args, app => $app);
26              
27             # Add "pro" handler
28 0           $app->renderer->add_handler(pro => $template);
29              
30             }
31              
32             sub build {
33 0     0 1   my $self = shift->SUPER::new(@_);
34 0           $self->_init(@_);
35 0     0     return sub { $self->_render(@_) }
36 0           }
37              
38             sub _init {
39 0     0     my $self = shift;
40 0           my %args = @_;
41              
42 0           my $mojo = delete $args{mojo};
43              
44             # now we only remember options
45 0           $self->pro($args{template_options});
46              
47 0           return $self;
48             }
49              
50             sub _render {
51 0     0     my ($self, $renderer, $c, $output, $options) = @_;
52              
53             # Inline
54 0           my $inline = $options->{inline};
55              
56             # Template
57 0           my $template = $renderer->template_name($options);
58 0 0         $template = 'inline'
59             if defined $inline;
60              
61 0 0         warn "render template: $template"
62             if $self->pro->{debug};
63              
64 0 0         unless ($template) {
65 0           $$output = 'some error happen!';
66 0           return 0;
67             }
68            
69             # try to get content
70 0           my $content = eval {
71 0   0       my $t = HTML::Template::Pro->new(
72             filename => $template,
73             # die_on_bad_params => 1,
74 0           %{$self->pro // {} }
75             );
76              
77             # filter mojo.* variables
78 0           my %params = map { $_ => $c->stash->{$_} }
  0            
79 0           grep !/mojo/, keys %{$c->stash};
80              
81 0           $t->param({%params, c => $c});
82 0           $t->output();
83             };
84              
85             # write error message to STDERR if eval fails
86             # and return with false
87 0 0 0       if($@ and $self->pro->{debug}) {
88 0           warn "ERROR: $@";
89 0           return 0;
90             }
91              
92             # return false if content empty
93 0 0         unless ($content) {
94             # write error message to output
95 0           $$output = "error while processing template: $template";
96 0           return 1;
97             }
98            
99             # assign content to $output ref
100 0           $$output = $content;
101            
102             # and return with true (success)
103 0           return 1;
104             }
105              
106             =head1 NAME
107              
108             Mojolicious::Plugin::ProRenderer - HTML::Tempate::Pro render plugin for Mojolicious
109              
110             =head1 VERSION
111              
112             Version 0.40
113              
114             =head1 SYNOPSIS
115              
116             Add the handler:
117              
118             *Mojolicious*
119            
120             sub startup {
121             ...
122             $self->plugin('pro_renderer');
123             ...
124             }
125              
126             in controller:
127              
128             $self->render(
129             message => 'test',
130             list => [
131             { id => 1 },
132             { id => 2 }
133             ]
134             );
135            
136             *Mojolicious::Lite*
137             ...
138             plugin pro_renderer;
139             ...
140             # or with options:
141             plugin pro_renderer => {
142             template_options => {
143             debug => 1,
144             ...
145             }
146             };
147            
148             =head2 OPTIONS
149              
150             options are the same as in HTML::Template::Pro constructor, added only debug
151              
152             =head1 FUNCTIONS
153              
154             =head2 build
155              
156             create handler for renderer
157              
158             =head2 register
159              
160             register plugin 'pro_renderer'
161              
162             =head1 ATTRIBUTES
163              
164             =head2 pro
165              
166             create attribute, containing template options
167              
168             =head1 AUTHOR
169              
170             Sergey Lobin, C<< >>
171              
172             =head1 BUGS
173              
174             Please report any bugs or feature requests to C, or through
175             the web interface at L. I will be notified, and then you'll
176             automatically be notified of progress on your bug as I make changes.
177              
178             =head1 SUPPORT
179              
180             You can find documentation for this module with the perldoc command.
181              
182             perldoc Mojolicious::Plugin::ProRenderer
183              
184              
185             You can also look for information at:
186              
187             =over 4
188              
189             =item * RT: CPAN's request tracker (report bugs here)
190              
191             L
192              
193             =item * AnnoCPAN: Annotated CPAN documentation
194              
195             L
196              
197             =item * CPAN Ratings
198              
199             L
200              
201             =item * Search CPAN
202              
203             L
204              
205             =back
206              
207              
208             =head1 ACKNOWLEDGEMENTS
209              
210              
211             =head1 LICENSE AND COPYRIGHT
212              
213             Copyright 2013 Sergey Lobin.
214              
215             This program is free software; you can redistribute it and/or modify it
216             under the terms of the the Artistic License (2.0). You may obtain a
217             copy of the full license at:
218              
219             L
220              
221             Any use, modification, and distribution of the Standard or Modified
222             Versions is governed by this Artistic License. By using, modifying or
223             distributing the Package, you accept this license. Do not use, modify,
224             or distribute the Package, if you do not accept this license.
225              
226             If your Modified Version has been derived from a Modified Version made
227             by someone other than you, you are nevertheless required to ensure that
228             your Modified Version complies with the requirements of this license.
229              
230             This license does not grant you the right to use any trademark, service
231             mark, tradename, or logo of the Copyright Holder.
232              
233             This license includes the non-exclusive, worldwide, free-of-charge
234             patent license to make, have made, use, offer to sell, sell, import and
235             otherwise transfer the Package with respect to any patent claims
236             licensable by the Copyright Holder that are necessarily infringed by the
237             Package. If you institute patent litigation (including a cross-claim or
238             counterclaim) against any party alleging that the Package constitutes
239             direct or contributory patent infringement, then this Artistic License
240             to you shall terminate on the date that such litigation is filed.
241              
242             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
243             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
244             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
245             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
246             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
247             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
248             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
249             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250              
251              
252             =cut
253              
254             1; # End of Mojolicious::Plugin::ProRenderer