line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::HTMLTemplateProRenderer; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
2106
|
use 5.006; |
|
4
|
|
|
|
|
11
|
|
4
|
4
|
|
|
4
|
|
13
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
27
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
2342
|
use HTML::Template::Pro; |
|
4
|
|
|
|
|
12217
|
|
|
4
|
|
|
|
|
167
|
|
7
|
4
|
|
|
4
|
|
1694
|
use HTML::Template::Pro::Extension; |
|
4
|
|
|
|
|
5174
|
|
|
4
|
|
|
|
|
2442
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
4
|
|
|
4
|
1
|
145
|
my ( $self, $app, $conf ) = @_; |
13
|
4
|
|
|
|
|
16
|
$self->{plugin_config} = $conf; |
14
|
|
|
|
|
|
|
$app->renderer->add_handler( tmpl => sub { |
15
|
7
|
|
|
7
|
|
58619
|
my ($r, $c, $out, $opt) = @_; |
16
|
|
|
|
|
|
|
# Fallback to ep template if pages raise exceptions |
17
|
|
|
|
|
|
|
return $r->handlers->{ep} |
18
|
7
|
50
|
|
|
|
43
|
if ($opt->{template} =~ /(exception)|(not_found)|(development)/); |
19
|
7
|
|
|
|
|
26
|
$self->render_tmpl(@_) |
20
|
4
|
|
|
|
|
23
|
} ); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub render_tmpl { |
24
|
7
|
|
|
7
|
0
|
12
|
my ( $self, $r, $c, $output, $options ) = @_; |
25
|
7
|
|
|
|
|
16
|
my $conf = $self->{plugin_config}; |
26
|
7
|
|
|
|
|
11
|
my %tmpl_params = %{ $c->stash }; |
|
7
|
|
|
|
|
17
|
|
27
|
|
|
|
|
|
|
|
28
|
2
|
|
|
|
|
7
|
unshift @{ $r->paths }, $c->app->home |
29
|
|
|
|
|
|
|
if ( $conf->{tmpl_opts}->{use_home_template} |
30
|
7
|
100
|
66
|
|
|
106
|
|| delete $tmpl_params{use_home_template} ); |
31
|
|
|
|
|
|
|
|
32
|
7
|
|
|
|
|
37
|
my $controller = $c->stash('controller'); |
33
|
|
|
|
|
|
|
|
34
|
7
|
|
|
|
|
45
|
my @template_dirs; |
35
|
|
|
|
|
|
|
|
36
|
7
|
|
|
|
|
17
|
push @template_dirs, $c->app->home->rel_dir('templates'); |
37
|
|
|
|
|
|
|
|
38
|
7
|
50
|
|
|
|
257
|
if ($controller) { |
39
|
0
|
|
|
|
|
0
|
push @template_dirs, $c->app->home->rel_dir("templates/$controller"); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
7
|
|
|
|
|
9
|
my $t; |
43
|
|
|
|
|
|
|
my %t_options; |
44
|
|
|
|
|
|
|
|
45
|
7
|
100
|
66
|
|
|
33
|
if ( $conf->{tmpl_opts}->{use_extension} |
46
|
|
|
|
|
|
|
|| delete $tmpl_params{use_extension} ) |
47
|
|
|
|
|
|
|
{ |
48
|
1
|
50
|
|
|
|
2
|
if ( defined( $options->{inline} ) ) { |
|
|
0
|
|
|
|
|
|
49
|
1
|
|
|
|
|
3
|
$t_options{tmplfile} = \$options->{inline}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
elsif ( defined( $options->{template} ) ) { |
52
|
0
|
0
|
|
|
|
0
|
if ( defined( my $path = $r->template_path($options) ) ) { |
53
|
0
|
|
|
|
|
0
|
$t_options{tmplfile} = $path; |
54
|
0
|
|
|
|
|
0
|
$t_options{cache} = 1; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else { |
57
|
0
|
|
|
|
|
0
|
$t_options{tmplfile} = \$r->get_data_template($options); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
50
|
|
|
5
|
my $plugins = $conf->{tmpl_opts}->{plugins} || []; |
62
|
1
|
|
|
|
|
2
|
$plugins = [ @$plugins, @{ $tmpl_params{plugins} } ] |
63
|
1
|
50
|
|
|
|
2
|
if exists $tmpl_params{plugins}; |
64
|
1
|
|
|
|
|
2
|
$t_options{plugins} = $plugins; |
65
|
1
|
|
|
|
|
9
|
$t = new HTML::Template::Pro::Extension(%t_options); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else { |
68
|
6
|
|
|
|
|
12
|
$t_options{die_on_bad_params} = 0; |
69
|
6
|
|
|
|
|
8
|
$t_options{global_vars} = 1; |
70
|
6
|
|
|
|
|
8
|
$t_options{loop_context_vars} = 1; |
71
|
6
|
|
|
|
|
8
|
$t_options{path} = \@template_dirs; |
72
|
6
|
|
|
|
|
11
|
$t_options{search_path_on_include} = 1; |
73
|
|
|
|
|
|
|
|
74
|
6
|
100
|
|
|
|
21
|
if ( defined( $options->{inline} ) ) { |
|
|
50
|
|
|
|
|
|
75
|
2
|
|
|
|
|
4
|
$t_options{scalarref} = \$options->{inline}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
elsif ( defined( $options->{template} ) ) { |
78
|
4
|
100
|
|
|
|
14
|
if ( defined( my $path = $r->template_path($options) ) ) { |
79
|
3
|
|
|
|
|
2708
|
$t_options{filename} = $path; |
80
|
3
|
|
|
|
|
5
|
$t_options{cache} = 1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
else { |
83
|
1
|
|
|
|
|
105
|
$t_options{scalarref} = $r->get_data_template($options); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
$t = HTML::Template::Pro->new( |
89
|
|
|
|
|
|
|
%t_options, |
90
|
6
|
50
|
|
|
|
20
|
%{ $conf->{tmpl_opts} || {} }, |
91
|
6
|
50
|
|
|
|
43
|
%{ delete $tmpl_params{tmpl_opts} || {} } |
|
6
|
|
|
|
|
67
|
|
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
7
|
50
|
|
|
|
1307
|
unless ($t) { $r->render_exception("ERROR: No template created"); } |
|
0
|
|
|
|
|
0
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# sanity params removing scalar inside arrayref |
98
|
7
|
|
|
|
|
19
|
foreach ( keys %tmpl_params ) { |
99
|
|
|
|
|
|
|
delete $tmpl_params{$_} |
100
|
|
|
|
|
|
|
if ( ( ref $tmpl_params{$_} eq 'ARRAY' ) |
101
|
|
|
|
|
|
|
&& $tmpl_params{$_} > 0 |
102
|
43
|
100
|
66
|
|
|
142
|
&& ref($tmpl_params{$_}->[0]) ne 'HASH' ); |
|
|
|
100
|
|
|
|
|
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
7
|
|
|
|
|
33
|
$t->param(%tmpl_params); |
106
|
|
|
|
|
|
|
|
107
|
7
|
|
|
|
|
222
|
$$output = $t->output(); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
__END__ |