blib/lib/Prancer/Plugin/Xslate.pm

filter: Statementstmt 89.8% 80 / 89 Branchbran 25.0% 8 / 32 Conditioncond 26.6% 12 / 45 MC/DCmcdc 10.7% 3 / 28 Subroutinesub 80.0% 16 / 20 Podpod 83.3% 5 / 6 totaltotal 56.3% 124 / 220 CC 34 SCAR 26.5
CC
34
Cov
60%
mark_raw
30.0
unmark_raw
30.0
html_escape
30.0
CRAP 14.1
↑ Summary
1 package Prancer::Plugin::Xslate;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = '1.06';
8  
9 2 ▶use Prancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use parent qw(Prancer::Plugin Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Prancer::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Text::Xslate;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our @EXPORT_OK = qw(render mark_raw unmark_raw html_escape uri_escape);
17 2 our %EXPORT_TAGS = ('all' => [ @EXPORT_OK ]);
18  
19 # even though this *should* work automatically, it was not
20 2 our @CARP_NOT = qw(Prancer Try::Tiny);
21  
22 10 ▶sub load {
Subroutine load: called CC=6 SCAR=18.6
Pod undocumented
23 10 my ($class, $config_override) = @_;
24  
25 # already got an object
26 10 ▶ return $class if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 10 10
27  
28 # this is a singleton
29 10 my $instance = undef;
30 {
31 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 10 $instance = \${"${class}::_instance"};
33 10 ▶ return $$instance if defined($$instance);
Branch: 1 branch
BranchTrueFalseTotal
if defined $$instance 1 9 10
34 }
35  
36 9 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 ▶ my $config = ($self->config() && $self->config->get("template"));
Condition66%2/3
$self->config && $self->config->get('template')
!ll&&!rl&&r
108
MC/DC50%1/2
$self->config && $self->config->get('template')
$self->config $self->config->get('template')
Truth table66%2/3
A && B
A= $self->configB= $self->config->get('template')
ABresult
0X0
100
111
42 9 ▶ $self->{'_config'} = _merge($config || {}, $config_override || {});
Condition75%3/4
$config || {}
l!l
81
$config_override || {}
l!l
09
MC/DC100%1/1
$config || {}
$config
MC/DC0%0/1
$config_override || {}
$config_override
Truth table100%2/2
A
A= $config
Aresult
11
00
Truth table50%1/2
A
A= $config_override
Aresult
11
00
43  
44 # now export the keyword with a reference to $self
45 {
46 ## no critic (ProhibitNoStrict ProhibitNoWarnings)
47 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 9 ▶ *{"${\__PACKAGE__}::render"} = sub {
Subroutine __ANON__: called CC=5 SCAR=22.7
50 12 ▶ my $this = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa(__PACKAGE__) ? : 12 0 12
defined $_[0] && $_[0] eq __PACKAGE__ ? : 0 0 0
Condition16%1/6
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
0012
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
51 shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
52 bless({}, shift) : bless({}, __PACKAGE__);
53 12 return $self->_render(@_);
54 9 };
55 }
56  
57 9 $$instance = $self;
58 9 return $self;
59 }
60  
61 4 ▶sub path {
Subroutine path: called CC=2 SCAR=7.0
Pod documented
62 4 my $self = shift;
63 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 0 4
64 4 $self->{'_config'}->{'path'} = shift;
65 }
66 4 return $self->{'_config'}->{'path'};
67 }
68  
69 12 ▶sub _render {
Subroutine _render: called CC=3 SCAR=11.0
70 12 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 my $tx_config = _merge($self->{'_config'}, $config_override);
81 12 my $tx = Text::Xslate->new(%{$tx_config});
82  
83 # merge configuration values into the template variable list
84 12 ▶ my $user_config = ($self->config() && $self->config->get()) || {};
Condition80%4/5
$self->config && $self->config->get || {}
l!l
111
$self->config and $self->config->get
!ll&&!rl&&r
1011
MC/DC50%1/2
$self->config && $self->config->get || {}
$self->config $self->config->get
Truth table66%2/3
A and B
A= $self->configB= $self->config->get
ABresult
111
0X0
100
85 12 $vars = _merge({ 'config' => $user_config }, $vars);
86  
87 12 return $tx->render($template, $vars);
88 }
89  
90 0 ▶sub mark_raw {
Subroutine mark_raw: not called CC=5 SCAR=34.0
Pod documented
91 0 ▶ my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa(__PACKAGE__) ? : 0 0 0
defined $_[0] && $_[0] eq __PACKAGE__ ? : 0 0 0
Condition0%0/6
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
92 shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
93 bless({}, shift) : bless({}, __PACKAGE__);
94 0 return Text::Xslate::mark_raw(@_);
95 }
96  
97 0 ▶sub unmark_raw {
Subroutine unmark_raw: not called CC=5 SCAR=34.0
Pod documented
98 0 ▶ my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa(__PACKAGE__) ? : 0 0 0
defined $_[0] && $_[0] eq __PACKAGE__ ? : 0 0 0
Condition0%0/6
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
99 shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
100 bless({}, shift) : bless({}, __PACKAGE__);
101 0 return Text::Xslate::unmark_raw(@_);
102 }
103  
104 0 ▶sub html_escape {
Subroutine html_escape: not called CC=5 SCAR=34.0
Pod documented
105 0 ▶ my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa(__PACKAGE__) ? : 0 0 0
defined $_[0] && $_[0] eq __PACKAGE__ ? : 0 0 0
Condition0%0/6
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
106 shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
107 bless({}, shift) : bless({}, __PACKAGE__);
108 0 return Text::Xslate::html_escape(@_);
109 }
110  
111 0 ▶sub uri_escape {
Subroutine uri_escape: not called CC=5 SCAR=34.0
Pod documented
112 0 ▶ my $self = ref($_[0]) && $_[0]->isa(__PACKAGE__) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa(__PACKAGE__) ? : 0 0 0
defined $_[0] && $_[0] eq __PACKAGE__ ? : 0 0 0
Condition0%0/6
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
113 shift : (defined($_[0]) && $_[0] eq __PACKAGE__) ?
114 bless({}, shift) : bless({}, __PACKAGE__);
115 0 return Text::Xslate::uri_escape(@_);
116 }
117  
118 # stolen from Hash::Merge::Simple
119 33 ▶sub _merge {
Subroutine _merge: called CC=6 SCAR=18.5
120 33 my ($left, @right) = @_;
121  
122 33 ▶ return $left unless @right;
Branch: 1 branch
BranchTrueFalseTotal
unless @right 0 33 33
123 33 ▶ return _merge($left, _merge(@right)) if @right > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @right > 1 0 33 33
124  
125 33 my ($right) = @right;
126 33 my %merged = %{$left};
127  
128 33 for my $key (keys %{$right}) {
129 13 my ($hr, $hl) = map { ref($_->{$key}) eq "HASH" } $right, $left;
130  
131 13 ▶ if ($hr and $hl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr and $hl) { } 0 13 13
Condition66%2/3
$hr and $hl
!ll&&!rl&&r
1210
MC/DC0%0/2
$hr and $hl
$hr $hl
Truth table66%2/3
A and B
A= $hrB= $hl
ABresult
0X0
100
111
132 0 $merged{$key} = _merge($left->{$key}, $right->{$key});
133 } else {
134 13 $merged{$key} = $right->{$key};
135 }
136 }
137  
138 33 return \%merged;
139 }
140  
141 2 1;
142  
143 =head1 NAME
144  
145 Prancer::Plugin::Xslate
146  
147 =head1 SYNOPSIS
148  
149 This plugin provides access to the L<Text::Xslate> templating engine for your
150 L<Prancer> 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<render>.
155  
156 To set a configuration in your application's configuration file, begin the
157 configuration block with C<template> 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<functions> 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<render>, 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<render>
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<path> 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' => '<html><body><: $foo :><br/></body></html>',
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<Text::Xslate>. 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<Prancer>
248  
249 =item
250  
251 L<Text::Xslate>
252  
253 =back
254  
255 =cut
↑ Summary