| 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")) // {}
$ds->config_value("engines")->{"template_toolkit"} // $ds->config_value("template_toolkit")
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")
| |||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$engine_attrs->{"INCLUDE_PATH"} // ""
$engine_attrs->{"INCLUDE_PATH"}
Truth table50%1/2
A
A= $engine_attrs->{"INCLUDE_PATH"}
| |||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | 7 | ▶ | my @include_list = ref $include_value eq 'ARRAY' ? @$include_value : split(/:/, $include_value); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||
| 74 | # Then create a template engine. | ||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | 7 | ▶ | my $tt = Template->new($engine_attrs) or croak(Template->error . "\n"); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 97 | 3 | ▶ | $base .= "<% PROCESS '$templates->{header}' %>\n" if $templates->{header}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 98 | 3 | ▶ | $base .= "<% PROCESS '$templates->{main}' %>\n" if $templates->{main}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 99 | 3 | ▶ | $base .= "<% PROCESS '$templates->{footer}' %>\n" if $templates->{footer}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||
| 112 | 3 | return $output; | |||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||
| 116 | 7 | 1; | |||||||||||||||||||||||