blib/lib/Web/DataService/Plugin/TemplateToolkit.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 60 CC 7 SCAR 28.6
CC
7
Cov
0%
render_template
30.0
new_engine
12.0
initialize_service
2.0
CRAP 17.5
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 use strict;
16  
17 package Web::DataService::Plugin::TemplateToolkit;
18  
19 use Carp qw( carp croak );
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 my ($plugin, $ds) = @_;
31
32 # Set the suffix used when auto-generating template file names from URL paths.
33
34 $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 my ($plugin, $ds, $attrs) = @_;
46
47 # Start with a set of default attributes.
48
49 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 my $tt_config = $ds->config_value('engines')->{template_toolkit} // $ds->config_value('template_toolkit') // {};
57
58 foreach my $key ( keys %$tt_config )
59 {
60 $engine_attrs->{$key} = $tt_config->{$key};
61 }
62
63 # Add the include directory specified by the parameters to this call.
64
65 my $include_value = $engine_attrs->{INCLUDE_PATH} // '';
66
67 my @include_list = ref $include_value eq 'ARRAY' ? @$include_value : split(/:/, $include_value);
68
69 unshift @include_list, $attrs->{template_dir};
70
71 $engine_attrs->{INCLUDE_PATH} = \@include_list;
72 $engine_attrs->{COMPILE_DIR} = $attrs->{compile_dir} if $attrs->{compile_dir};
73
74 # Then create a template engine.
75
76 my $tt = Template->new($engine_attrs) or croak(Template->error . "\n");
77
78 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 my ($plugin, $ds, $engine, $vars, $templates) = @_;
90
91 # First, determine the list of templates to render, by unpacking the
92 # $templates hash.
93
94 my $base = '';
95
96 $base .= "<% PROCESS '$templates->{defs}' %>\n" if $templates->{defs};
97 $base .= "<% PROCESS '$templates->{header}' %>\n" if $templates->{header};
98 $base .= "<% PROCESS '$templates->{main}' %>\n" if $templates->{main};
99 $base .= "<% PROCESS '$templates->{footer}' %>\n" if $templates->{footer};
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 my $output = '';
109
110 $engine->process(\$base, $vars, \$output) or croak($engine->error . "\n");
111
112 return $output;
113 }
114  
115  
116 1;