Neaf/View/TT.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 87.5% 14 / 16 Conditioncond 46.6% 7 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 85.0% 102 / 120 CC 19 SCAR 19.5
CC
19
Cov
89%
new
13.2
render
5.0
preload
3.0
CRAP 7.0
« Neaf/Route/Main.pm ↑ Summary Neaf/Exception.pm »
1 package MVC::Neaf::View::TT;
2  
3 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 our $VERSION = '0.2901';
8  
9 =head1 NAME
10  
11 MVC::Neaf::View::TT - Template toolkit-based view module for Neaf.
12  
13 =head1 SYNOPSYS
14  
15 # Somewhere in your app
16 return {
17 -view => 'TT',
18 -template => 'foo.tt',
19 title => 'Page title',
20 ....
21 };
22 # Neaf knows it's time to render foo.tt with returned data as stash
23 # and return result to user
24  
25 # What actually happens
26 my $view = MVC::Neaf::View::TT->new;
27 my $content = $view->render( { ... } );
28  
29 # And if in foo.tt
30 <title>[% title %]</title>
31  
32 # Then in $content it becomes
33 <title>Page title</title>
34  
35 =head1 DESCRIPTION
36  
37 This module is one of core rendering engines of L<MVC::Neaf>
38 known under C<TT> alias.
39  
40 See also C<neaf view>.
41  
42 =head1 METHODS
43  
44 =cut
45  
46 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 23 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 23 ▶use Template::Provider;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 23 our @CARP_NOT = qw(MVC::Neaf::View MVC::Neaf MVC::Neaf::Request);
51  
52 23 ▶use parent qw(MVC::Neaf::View);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 =head2 new( %options )
55  
56 %options may include:
57  
58 =over
59  
60 =item * template - default template to use.
61  
62 =item * preserve_dash - don't strip dashed options. Useful for debugging.
63  
64 =item * preload => { name => 'in-memory template' } - preload some templates.
65 See C<preload()> below.
66  
67 =item * INCLUDE_PATH => [path, ...] - will be calculated relative
68 to the calling file, if not starts with a '/'.
69  
70 =back
71  
72 Also any UPPERCASE OPTIONS will be forwarded to the backend
73 (i.e. Template object) w/o changes.
74  
75 Any extra options except those above will cause an exception.
76  
77 =cut
78  
79 23 my %new_opt;
80 23 $new_opt{$_}++ for qw( template preserve_dash engine preload preload_auto neaf_base_dir );
81  
82 23 my @opt_provider = qw(
83 INCLUDE_PATH ABSOLUTE RELATIVE DEFAULT ENCODING CACHE_SIZE STAT_TTL
84 COMPILE_EXT COMPILE_DIR TOLERANT PARSER DEBUG EVAL_PERL
85 );
86  
87 17 ▶sub new {
Subroutine new: called CC=12 SCAR=25.8
Pod documented
88 17 my ($class, %opt) = @_;
89  
90 17 my %tt_opt;
91 $tt_opt{$_} = delete $opt{$_}
92 17 for grep { /^[A-Z]/ } keys %opt;
93 17 my @extra = grep { !$new_opt{$_} } keys %opt;
94 17 ▶ croak( "$class->new: Unknown options @extra" )
Branch: 1 branch
BranchTrueFalseTotal
if @extra 0 17 17
95 if @extra;
96  
97 17 my $engine = delete $opt{engine};
98 17 my $preload = delete $opt{preload};
99 17 my $self = $class->SUPER::new(%opt);
100  
101 17 ▶ $self->{engine} ||= do {
Condition33%1/3
$self->{"engine"} ||= do { $tt_opt{"INCLUDE_PATH"} = $self->dir($tt_opt{"INCLUDE_PATH"} || []); my %prov_opt; foreach $_ (@opt_provider) { $prov_opt{$_} = $tt_opt{$_} if defined $tt_opt{$_}; } ; my $prov = delete $tt_opt{"LOAD_TEMPLATES"} || ["Template::Provider"->new(\%prov_opt)]; $self->{"engine_preload"} = "Template::Provider"->new({%prov_opt, "CACHE_SIZE", undef, "STAT_TTL", 4000000000}); $prov = [$self->{"engine_preload"}, @$prov]; "Template"->new(%tt_opt, "LOAD_TEMPLATES", $prov) }
l!l&&r!l&&!r
0170
MC/DC0%0/2
$self->{"engine"} ||= do { $tt_opt{"INCLUDE_PATH"} = $self->dir($tt_opt{"INCLUDE_PATH"} || []); my %prov_opt; foreach $_ (@opt_provider) { $prov_opt{$_} = $tt_opt{$_} if defined $tt_opt{$_}; } ; my $prov = delete $tt_opt{"LOAD_TEMPLATES"} || ["Template::Provider"->new(\%prov_opt)]; $self->{"engine_preload"} = "Template::Provider"->new({%prov_opt, "CACHE_SIZE", undef, "STAT_TTL", 4000000000}); $prov = [$self->{"engine_preload"}, @$prov]; "Template"->new(%tt_opt, "LOAD_TEMPLATES", $prov) }
$self->{"engine"} do { $tt_opt{"INCLUDE_PATH"} = $self->dir($tt_opt{"INCLUDE_PATH"} || []); my %prov_opt; foreach $_ (@opt_provider) { $prov_opt{$_} = $tt_opt{$_} if defined $tt_opt{$_}; } ; my $prov = delete $tt_opt{"LOAD_TEMPLATES"} || ["Template::Provider"->new(\%prov_opt)]; $self->{"engine_preload"} = "Template::Provider"->new({%prov_opt, "CACHE_SIZE", undef, "STAT_TTL", 4000000000}); $prov = [$self->{"engine_preload"}, @$prov]; "Template"->new(%tt_opt, "LOAD_TEMPLATES", $prov) }
Truth table33%1/3
A ||= B
A= $self->{"engine"}B= do { $tt_opt{"INCLUDE_PATH"} = $self->dir($tt_opt{"INCLUDE_PATH"} || []); my %prov_opt; foreach $_ (@opt_provider) { $prov_opt{$_} = $tt_opt{$_} if defined $tt_opt{$_}; } ; my $prov = delete $tt_opt{"LOAD_TEMPLATES"} || ["Template::Provider"->new(\%prov_opt)]; $self->{"engine_preload"} = "Template::Provider"->new({%prov_opt, "CACHE_SIZE", undef, "STAT_TTL", 4000000000}); $prov = [$self->{"engine_preload"}, @$prov]; "Template"->new(%tt_opt, "LOAD_TEMPLATES", $prov) }
ABresult
1X1
011
000
102 # enforce non-absolute paths to be calculated relative to caller file
103 17 ▶ $tt_opt{INCLUDE_PATH} = $self->dir( $tt_opt{INCLUDE_PATH} || [] );
Condition100%2/2
$tt_opt{'INCLUDE_PATH'} || []
l!l
314
MC/DC100%1/1
$tt_opt{'INCLUDE_PATH'} || []
$tt_opt{'INCLUDE_PATH'}
Truth table100%2/2
A
A= $tt_opt{'INCLUDE_PATH'}
Aresult
11
00
104  
105 17 my %prov_opt;
106 17 foreach( @opt_provider) {
107 $prov_opt{$_} = $tt_opt{$_}
108 221 ▶ if defined $tt_opt{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tt_opt{$_} 20 201 221
109 };
110  
111 my $prov = delete $tt_opt{LOAD_TEMPLATES} || [
112 17 ▶ Template::Provider->new(\%prov_opt)
Condition50%1/2
delete $tt_opt{'LOAD_TEMPLATES'} || ['Template::Provider'->new(\%prov_opt)]
l!l
017
MC/DC0%0/1
delete $tt_opt{'LOAD_TEMPLATES'} || ['Template::Provider'->new(\%prov_opt)]
delete $tt_opt{'LOAD_TEMPLATES'}
Truth table50%1/2
A
A= delete $tt_opt{'LOAD_TEMPLATES'}
Aresult
11
00
113 ];
114 17 $self->{engine_preload} = Template::Provider->new({
115 %prov_opt,
116 CACHE_SIZE => undef,
117 STAT_TTL => 4_000_000_000,
118 });
119 # shallow copy (not unshift) to avoid spoiling original values
120 17 $prov = [ $self->{engine_preload}, @$prov ];
121  
122 17 Template->new (%tt_opt, LOAD_TEMPLATES => $prov);
123 };
124  
125  
126 # TODO 0.40 automagically preload from the calling file's DATA section
127 17 ▶ if ( ref $preload eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $preload eq 'HASH') { } 1 16 17
elsif ($preload) { } 0 16 16
128 $self->preload( $_ => $preload->{$_} )
129 1 for keys %$preload;
130 } elsif ($preload) {
131 0 ▶ $self->_croak("preload must be a hash, not ".(ref $preload || "a scalar") );
Condition0%0/2
ref $preload || 'a scalar'
l!l
00
MC/DC0%0/1
ref $preload || 'a scalar'
ref $preload
Truth table0%0/2
A
A= ref $preload
Aresult
11
00
132 };
133 17 return $self;
134 };
135  
136 =head2 render( \%data )
137  
138 Returns a pair of values: ($content, $content_type).
139  
140 Content-type defaults to text/html.
141  
142 The template is determined from (1) -template in data (2) template in new().
143 If neither is present, empty string and "text/plain" are returned.
144  
145 =cut
146  
147 21 ▶sub render {
Subroutine render: called CC=5 SCAR=16.1
Pod documented
148 21 my ($self, $data) = @_;
149  
150 21 ▶ my $template = $data->{-template} || $self->{template};
Condition66%2/3
$data->{'-template'} || $self->{'template'}
l!l&&r!l&&!r
2001
MC/DC50%1/2
$data->{'-template'} || $self->{'template'}
$data->{'-template'} $self->{'template'}
Truth table66%2/3
A || B
A= $data->{'-template'}B= $self->{'template'}
ABresult
1X1
011
000
151  
152 21 ▶ if (!defined $template) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $template) 1 20 21
153 1 croak __PACKAGE__.": -template option is required";
154 };
155  
156 20 my $out;
157 $self->{engine}->process( $template, $data, \$out )
158 20 ▶ or $self->_croak( $self->{engine}->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"engine"}->process($template, $data, \$out) or $self->_croak($self->{"engine"}->error) 16 4 20
159  
160 16 ▶ return wantarray ? ($out, "text/html") : $out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 1 16
161 };
162  
163 =head2 preload ( name => "[% in_memory_template %]", ... )
164  
165 Store precompiled templates under given names.
166  
167 Returns self, dies on error.
168  
169 =cut
170  
171 6 ▶sub preload {
Subroutine preload: called CC=3 SCAR=11.0
Pod documented
172 6 my ($self, %tpls) = @_;
173  
174 6 foreach (keys %tpls) {
175 9 ▶ my $compiled = eval { $self->{engine}->template( \$tpls{$_} ) }
Branch: 1 branch
BranchTrueFalseTotal
my $compiled = eval { do { $self->{"engine"}->template(\$tpls{$_}) } } or $self->_croak("${_}: $@") 8 1 9
176 or $self->_croak( "$_: $@" );
177  
178 8 $self->{engine_preload}->store( $_, $compiled );
179 };
180  
181 5 return $self;
182 };
183  
184 5 ▶sub _croak {
Subroutine _croak: called CC=2 SCAR=7.4
185 5 my ($self, @msg) = @_;
186  
187 5 my $where = [caller(1)]->[3];
188 5 $where =~ s/.*:://;
189  
190 5 ▶ croak join "", (ref $self || $self), '->', $where, "(): ", @msg;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
191 };
192  
193 =head1 SEE ALSO
194  
195 L<Template> - the template toolkit used as backend.
196  
197 =head1 LICENSE AND COPYRIGHT
198  
199 This module is part of L<MVC::Neaf> suite.
200  
201 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
202  
203 This program is free software; you can redistribute it and/or modify it
204 under the terms of either: the GNU General Public License as published
205 by the Free Software Foundation; or the Artistic License.
206  
207 See L<http://dev.perl.org/licenses/> for more information.
208  
209 =cut
210  
211 23 1;
« Neaf/Route/Main.pm ↑ Summary Neaf/Exception.pm »