/home/cpancover/.cpan/build/Web-DataService-0.41-0/lib/Web/DataService/Plugin/TemplateToolkit.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 50.0% 8 / 16 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 3 totaltotal 70.3% 45 / 64 CC 13 SCAR 19.8
CC
13
Cov
75%
new_engine
10.1
render_template
6.7
BEGIN
1.0
CRAP 7.3
« blib/lib/Web/DataService/IDocument.pm ↑ Summary Build »
1 #
2 # Web::DataService::Plugin::TemplateToolkit.pm
3 #
4 # This plugin provides Web::DataService with the ability to use Template.pm as
5 # its templating engine. Web::DataService uses the foundation framework
6 # to parse HTTP requests, to marshall and send back HTTP responses, and to
7 # provide configuration information for the data service.
8 #
9 # Other plugins will eventually be developed to fill this same role using
10 # Mojolicious and other frameworks.
11 #
12 # Author: Michael McClennen <mmcclenn@cpan.org>
13  
14  
15 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 package Web::DataService::Plugin::TemplateToolkit;
18  
19 7 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23 # initialize_service ( ds )
24 #
25 # This method is called automatically whenever a new data service instance is
26 # created.
27  
28 sub initialize_service {
29
30 7 ▶ my ($plugin, $ds) = @_;
Subroutine initialize_service: called CC=1 SCAR=0.0
Pod undocumented
31
32 # Set the suffix used when auto-generating template file names from URL paths.
33
34 7 $ds->{template_suffix} = '.tt';
35 }
36  
37  
38 # new_engine ( ds, config, attrs )
39 #
40 # This method is called automatically to initialize the necessary template
41 # processing engines.
42  
43 sub new_engine {
44
45 7 ▶ my ($plugin, $ds, $attrs) = @_;
Subroutine new_engine: called CC=8 SCAR=23.1
Pod undocumented
46
47 # Start with a set of default attributes.
48
49 7 my $engine_attrs = {
50 TAG_STYLE => 'asp',
51 EVAL_PERL => 1,
52 };
53
54 # These can be overridden by attributes in the configuration file.
55
56 7 ▶ my $tt_config = $ds->config_value('engines')->{template_toolkit} // $ds->config_value('template_toolkit') // {};
Condition40%2/5
($ds->config_value("engines")->{"template_toolkit"} // $ds->config_value("template_toolkit")) // {}
l!l
07
$ds->config_value("engines")->{"template_toolkit"} // $ds->config_value("template_toolkit")
l!l&&r!l&&!r
007
MC/DC0%0/2
($ds->config_value("engines")->{"template_toolkit"} // $ds->config_value("template_toolkit")) // {}
$ds->config_value("engines")->{"template_toolkit"} $ds->config_value("template_toolkit")
Truth table33%1/3
A // B
A= $ds->config_value("engines")->{"template_toolkit"}B= $ds->config_value("template_toolkit")
ABresult
1X1
011
000
57
58 7 foreach my $key ( keys %$tt_config )
59 {
60 0 $engine_attrs->{$key} = $tt_config->{$key};
61 }
62
63 # Add the include directory specified by the parameters to this call.
64
65 7 ▶ my $include_value = $engine_attrs->{INCLUDE_PATH} // '';
Condition50%1/2
$engine_attrs->{"INCLUDE_PATH"} // ""
l!l
07
MC/DC0%0/1
$engine_attrs->{"INCLUDE_PATH"} // ""
$engine_attrs->{"INCLUDE_PATH"}
Truth table50%1/2
A
A= $engine_attrs->{"INCLUDE_PATH"}
Aresult
11
00
66
67 7 ▶ my @include_list = ref $include_value eq 'ARRAY' ? @$include_value : split(/:/, $include_value);
Branch: 1 branch
BranchTrueFalseTotal
ref $include_value eq 'ARRAY' ? : 0 7 7
68
69 7 unshift @include_list, $attrs->{template_dir};
70
71 7 $engine_attrs->{INCLUDE_PATH} = \@include_list;
72 7 ▶ $engine_attrs->{COMPILE_DIR} = $attrs->{compile_dir} if $attrs->{compile_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"compile_dir"} 0 7 7
73
74 # Then create a template engine.
75
76 7 ▶ my $tt = Template->new($engine_attrs) or croak(Template->error . "\n");
Branch: 1 branch
BranchTrueFalseTotal
my $tt = "Template"->new($engine_attrs) or croak("Template"->error . "\n") 0 7 7
77
78 7 return $tt;
79 }
80  
81  
82 # render_template ( plugin, ds, engine, vars, templates )
83 #
84 # Render the specified template (preceded by defs and/or header, and followed
85 # by footer) using the specified variables.
86  
87 sub render_template {
88  
89 3 ▶ my ($plugin, $ds, $engine, $vars, $templates) = @_;
Subroutine render_template: called CC=6 SCAR=19.0
Pod undocumented
90
91 # First, determine the list of templates to render, by unpacking the
92 # $templates hash.
93
94 3 my $base = '';
95
96 3 ▶ $base .= "<% PROCESS '$templates->{defs}' %>\n" if $templates->{defs};
Branch: 1 branch
BranchTrueFalseTotal
if $templates->{"defs"} 3 0 3
97 3 ▶ $base .= "<% PROCESS '$templates->{header}' %>\n" if $templates->{header};
Branch: 1 branch
BranchTrueFalseTotal
if $templates->{"header"} 3 0 3
98 3 ▶ $base .= "<% PROCESS '$templates->{main}' %>\n" if $templates->{main};
Branch: 1 branch
BranchTrueFalseTotal
if $templates->{"main"} 3 0 3
99 3 ▶ $base .= "<% PROCESS '$templates->{footer}' %>\n" if $templates->{footer};
Branch: 1 branch
BranchTrueFalseTotal
if $templates->{"footer"} 3 0 3
100
101 # Then construct a special template to render them in order.
102
103 #my $list = join(' + ', @templates);
104 #my $base = "<% PROCESS $list %>";
105
106 # Process it and return the result.
107
108 3 my $output = '';
109
110 3 ▶ $engine->process(\$base, $vars, \$output) or croak($engine->error . "\n");
Branch: 1 branch
BranchTrueFalseTotal
$engine->process(\$base, $vars, \$output) or croak($engine->error . "\n") 0 3 3
111
112 3 return $output;
113 }
114  
115  
116 7 1;
« blib/lib/Web/DataService/IDocument.pm ↑ Summary Build »