line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Prancer::Plugin::Xslate; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
47135
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
7
|
use warnings FATAL => 'all'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
68
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
406
|
use version; |
|
2
|
|
|
|
|
1372
|
|
|
2
|
|
|
|
|
11
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.06'; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
972
|
use Prancer::Plugin; |
|
2
|
|
|
|
|
16408
|
|
|
2
|
|
|
|
|
53
|
|
10
|
2
|
|
|
2
|
|
10
|
use parent qw(Prancer::Plugin Exporter); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
7
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
91
|
use Prancer::Core; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
13
|
2
|
|
|
2
|
|
1132
|
use Text::Xslate; |
|
2
|
|
|
|
|
15108
|
|
|
2
|
|
|
|
|
87
|
|
14
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
239
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = qw(render mark_raw unmark_raw html_escape uri_escape); |
17
|
|
|
|
|
|
|
our %EXPORT_TAGS = ('all' => [ @EXPORT_OK ]); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# even though this *should* work automatically, it was not |
20
|
|
|
|
|
|
|
our @CARP_NOT = qw(Prancer Try::Tiny); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub load { |
23
|
10
|
|
|
10
|
0
|
156962
|
my ($class, $config_override) = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# already got an object |
26
|
10
|
50
|
|
|
|
35
|
return $class if ref($class); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# this is a singleton |
29
|
10
|
|
|
|
|
15
|
my $instance = undef; |
30
|
|
|
|
|
|
|
{ |
31
|
2
|
|
|
2
|
|
10
|
no strict 'refs'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
215
|
|
|
10
|
|
|
|
|
9
|
|
32
|
10
|
|
|
|
|
12
|
$instance = \${"${class}::_instance"}; |
|
10
|
|
|
|
|
34
|
|
33
|
10
|
100
|
|
|
|
31
|
return $$instance if defined($$instance); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
9
|
|
|
|
|
18
|
my $self = bless({}, $class); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# merge together options from the configuration file ($config) with options |
39
|
|
|
|
|
|
|
# given to this method ($options). later this set of configuration options |
40
|
|
|
|
|
|
|
# will be merged with any options given to "render". |
41
|
9
|
|
66
|
|
|
41
|
my $config = ($self->config() && $self->config->get("template")); |
42
|
9
|
|
100
|
|
|
927
|
$self->{'_config'} = _merge($config || {}, $config_override || {}); |
|
|
|
50
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# now export the keyword with a reference to $self |
45
|
|
|
|
|
|
|
{ |
46
|
|
|
|
|
|
|
## no critic (ProhibitNoStrict ProhibitNoWarnings) |
47
|
2
|
|
|
2
|
|
23
|
no strict 'refs'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
49
|
|
|
9
|
|
|
|
|
13
|
|
48
|
2
|
|
|
2
|
|
7
|
no warnings 'redefine'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1203
|
|
49
|
9
|
|
|
|
|
10
|
*{"${\__PACKAGE__}::render"} = sub { |
|
9
|
|
|
|
|
109
|
|
50
|
12
|
0
|
33
|
12
|
|
7919
|
my $this = ref($_[0]) && $_[0]->isa(__PACKAGE__) ? |
|
|
50
|
0
|
|
|
|
|
51
|
|
|
|
|
|
|
shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ? |
52
|
|
|
|
|
|
|
bless({}, shift) : bless({}, __PACKAGE__); |
53
|
12
|
|
|
|
|
32
|
return $self->_render(@_); |
54
|
9
|
|
|
|
|
54
|
}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
9
|
|
|
|
|
15
|
$$instance = $self; |
58
|
9
|
|
|
|
|
25
|
return $self; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub path { |
62
|
4
|
|
|
4
|
1
|
18
|
my $self = shift; |
63
|
4
|
50
|
|
|
|
12
|
if (@_) { |
64
|
4
|
|
|
|
|
37
|
$self->{'_config'}->{'path'} = shift; |
65
|
|
|
|
|
|
|
} |
66
|
4
|
|
|
|
|
8
|
return $self->{'_config'}->{'path'}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _render { |
70
|
12
|
|
|
12
|
|
16
|
my ($self, $template, $vars, $config_override) = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# just pass all of the options directly to Text::Xslate |
73
|
|
|
|
|
|
|
# some default options that are important to remember: |
74
|
|
|
|
|
|
|
# cache = 1 |
75
|
|
|
|
|
|
|
# cache_dir = $ENV{'HOME'}/.xslate_cache |
76
|
|
|
|
|
|
|
# verbose = 1 |
77
|
|
|
|
|
|
|
# suffix = '.tx' |
78
|
|
|
|
|
|
|
# syntax = 'Kolon' |
79
|
|
|
|
|
|
|
# type = 'html' (identical to xml) |
80
|
12
|
|
|
|
|
56
|
my $tx_config = _merge($self->{'_config'}, $config_override); |
81
|
12
|
|
|
|
|
12
|
my $tx = Text::Xslate->new(%{$tx_config}); |
|
12
|
|
|
|
|
75
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# merge configuration values into the template variable list |
84
|
12
|
|
100
|
|
|
8740
|
my $user_config = ($self->config() && $self->config->get()) || {}; |
85
|
12
|
|
|
|
|
640
|
$vars = _merge({ 'config' => $user_config }, $vars); |
86
|
|
|
|
|
|
|
|
87
|
12
|
|
|
|
|
102
|
return $tx->render($template, $vars); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub mark_raw { |
91
|
0
|
0
|
0
|
0
|
1
|
0
|
my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ? |
|
|
0
|
0
|
|
|
|
|
92
|
|
|
|
|
|
|
shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ? |
93
|
|
|
|
|
|
|
bless({}, shift) : bless({}, __PACKAGE__); |
94
|
0
|
|
|
|
|
0
|
return Text::Xslate::mark_raw(@_); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub unmark_raw { |
98
|
0
|
0
|
0
|
0
|
1
|
0
|
my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ? |
|
|
0
|
0
|
|
|
|
|
99
|
|
|
|
|
|
|
shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ? |
100
|
|
|
|
|
|
|
bless({}, shift) : bless({}, __PACKAGE__); |
101
|
0
|
|
|
|
|
0
|
return Text::Xslate::unmark_raw(@_); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub html_escape { |
105
|
0
|
0
|
0
|
0
|
1
|
0
|
my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ? |
|
|
0
|
0
|
|
|
|
|
106
|
|
|
|
|
|
|
shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ? |
107
|
|
|
|
|
|
|
bless({}, shift) : bless({}, __PACKAGE__); |
108
|
0
|
|
|
|
|
0
|
return Text::Xslate::html_escape(@_); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub uri_escape { |
112
|
0
|
0
|
0
|
0
|
1
|
0
|
my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ? |
|
|
0
|
0
|
|
|
|
|
113
|
|
|
|
|
|
|
shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ? |
114
|
|
|
|
|
|
|
bless({}, shift) : bless({}, __PACKAGE__); |
115
|
0
|
|
|
|
|
0
|
return Text::Xslate::uri_escape(@_); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# stolen from Hash::Merge::Simple |
119
|
|
|
|
|
|
|
sub _merge { |
120
|
33
|
|
|
33
|
|
49
|
my ($left, @right) = @_; |
121
|
|
|
|
|
|
|
|
122
|
33
|
50
|
|
|
|
58
|
return $left unless @right; |
123
|
33
|
50
|
|
|
|
65
|
return _merge($left, _merge(@right)) if @right > 1; |
124
|
|
|
|
|
|
|
|
125
|
33
|
|
|
|
|
33
|
my ($right) = @right; |
126
|
33
|
|
|
|
|
24
|
my %merged = %{$left}; |
|
33
|
|
|
|
|
111
|
|
127
|
|
|
|
|
|
|
|
128
|
33
|
|
|
|
|
34
|
for my $key (keys %{$right}) { |
|
33
|
|
|
|
|
82
|
|
129
|
13
|
|
|
|
|
15
|
my ($hr, $hl) = map { ref($_->{$key}) eq "HASH" } $right, $left; |
|
26
|
|
|
|
|
61
|
|
130
|
|
|
|
|
|
|
|
131
|
13
|
50
|
66
|
|
|
41
|
if ($hr and $hl) { |
132
|
0
|
|
|
|
|
0
|
$merged{$key} = _merge($left->{$key}, $right->{$key}); |
133
|
|
|
|
|
|
|
} else { |
134
|
13
|
|
|
|
|
37
|
$merged{$key} = $right->{$key}; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
33
|
|
|
|
|
82
|
return \%merged; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 NAME |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Prancer::Plugin::Xslate |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 SYNOPSIS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This plugin provides access to the L templating engine for your |
150
|
|
|
|
|
|
|
L application and exports a keyword to access the configured engine. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This template plugin supports setting the basic configuration in your Prancer |
153
|
|
|
|
|
|
|
application's configuration file. You can also configure all options at runtime |
154
|
|
|
|
|
|
|
using arguments to C. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
To set a configuration in your application's configuration file, begin the |
157
|
|
|
|
|
|
|
configuration block with C and put all options underneath that. For |
158
|
|
|
|
|
|
|
example: |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
template: |
161
|
|
|
|
|
|
|
cache_dir: /path/to/cache |
162
|
|
|
|
|
|
|
verbose: 2 |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Any option for Text::Xslate whose value can be expressed in a configuration |
165
|
|
|
|
|
|
|
file can be put into your application's configuration. Then using the template |
166
|
|
|
|
|
|
|
engine is as simple as this: |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
use Prancer::Plugin::Xslate qw(render); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Prancer::Plugin::Xslate->load(); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
print render("foobar.tx", \%vars); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
However, there are some configuration options that cannot be expressed in |
175
|
|
|
|
|
|
|
configuration files, especially the C option. There are two |
176
|
|
|
|
|
|
|
additional ways to handle that. The first way is to pass them to the template |
177
|
|
|
|
|
|
|
plugin when loading it, like this: |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Prancer::Plugin::Xslate->load({ |
180
|
|
|
|
|
|
|
'function' => { |
181
|
|
|
|
|
|
|
'encode_json' => sub { |
182
|
|
|
|
|
|
|
return JSON::encode_json(@_); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
}); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
The second way is to the optional third argument to C, like this: |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
print render("foobar.tx", \%vars, { |
190
|
|
|
|
|
|
|
'function' => { |
191
|
|
|
|
|
|
|
'md5_hex' => sub { |
192
|
|
|
|
|
|
|
return Digest::MD5::md5_hex(@_); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
}); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Options passed when initializing the template plugin will override options |
198
|
|
|
|
|
|
|
configured in the configuration file. Options passed when calling C |
199
|
|
|
|
|
|
|
will override options passed when initializing the template plugin. This is |
200
|
|
|
|
|
|
|
the way you might go about adding support for functions and methods. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 METHODS |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=over |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item path |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
This will set the C option for Text::Xslate to anything that Text::Xslate |
209
|
|
|
|
|
|
|
suports. Each call to this method will overwrite whatever the previous template |
210
|
|
|
|
|
|
|
path was. For example: |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
# sets template path to just /path/to/templates |
213
|
|
|
|
|
|
|
$plugin->path('/path/to/templates'); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# blows away /path/to/templates set previously and sets it to this arrayref |
216
|
|
|
|
|
|
|
$plugin->path([ '/path/to/global-templates', '/path/to/local-templates' ]); |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# blows away the arrayref set previously and sets it to this hashref |
219
|
|
|
|
|
|
|
$plugin->path({ |
220
|
|
|
|
|
|
|
'foo.tx' => '<: $foo :> ', |
221
|
|
|
|
|
|
|
'bar.tx' => 'Hello, <: $bar :>.', |
222
|
|
|
|
|
|
|
}); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item mark_raw, unmark_raw, html_escape, uri_escape |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Proxies access to the static functions of the same name provided in |
227
|
|
|
|
|
|
|
L. These can all be called statically or an instance of the |
228
|
|
|
|
|
|
|
plugin and all will work just fine. All of these can also be exported on |
229
|
|
|
|
|
|
|
demand. For more information on how to use these functions, read the |
230
|
|
|
|
|
|
|
Text::Xslate documentation. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=back |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 COPYRIGHT |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Copyright 2014, 2015 Paul Lockaby. All rights reserved. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under |
239
|
|
|
|
|
|
|
the same terms as Perl itself. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head1 SEE ALSO |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=over |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=item |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
L |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
L |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=back |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |