View/TT/Alloy.pm

filter: Statementstmt 86.8% 79 / 91 Branchbran 61.7% 21 / 34 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 76.2% 119 / 156 CC 21 SCAR 20.1
CC
21
Cov
77%
process
19.7
_coerce_paths
5.5
new
5.1
CRAP 7.5
↑ Summary Helper/View/TT/Alloy.pm »
1 #!/bin/false
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Catalyst::View::TT::Alloy;
7 5 $Catalyst::View::TT::Alloy::VERSION = '0.00007';
8 5 ▶use parent qw( Catalyst::View );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Scalar::Util qw( weaken blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Template::Alloy qw( Compile Parse TT );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 __PACKAGE__->mk_accessors('template');
17 5 __PACKAGE__->mk_accessors('include_path');
18  
19 =head1 NAME
20  
21 Catalyst::View::TT::Alloy - Template::Alloy (TT) View Class
22  
23 =head1 VERSION
24  
25 version 0.00007
26  
27 =head1 SYNOPSIS
28  
29 # use the helper to create your View
30 myapp_create.pl view TT::Alloy TT::Alloy
31  
32 # configure in myapp.yml
33  
34 'View::TT::Alloy':
35 INCLUDE_PATH:
36 - __path_to(root/src)__
37 - __path_to(root/lib)__
38 PRE_PROCESS: 'config/main'
39 WRAPPER: 'site/wrapper'
40 # optional
41 TEMPLATE_EXTENSION: '.tt'
42 CATALYST_VAR: 'Catalyst'
43  
44 # example render view in lib/MyApp/Controller/Root.pm
45  
46 sub default : Private {
47 my ( $self, $c ) = @_;
48 $c->stash->{template} = 'message.tt2';
49 $c->stash->{message} = 'Hello World!';
50 return;
51 }
52  
53 sub end : ActionClass('RenderView') {
54 }
55  
56 # access variables from template
57  
58 The message is: [% message %].
59  
60 # example when CATALYST_VAR is set to 'Catalyst'
61 Context is [% Catalyst %]
62 The base is [% Catalyst.req.base %]
63 The name is [% Catalyst.config.name %]
64  
65 # example when CATALYST_VAR isn't set
66 Context is [% c %]
67 The base is [% base %]
68 The name is [% name %]
69  
70 =cut
71  
72 16 ▶sub _coerce_paths {
Subroutine _coerce_paths: called CC=5 SCAR=17.0
73 16 my ( $paths, $dlim ) = shift;
74 16 ▶ return () if ( !$paths );
Branch: 1 branch
BranchTrueFalseTotal
unless $paths 8 8 16
75 0 ▶ return @{$paths} if ( ref $paths eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $paths eq "ARRAY" 0 8 8
76  
77 # tweak delim to ignore C:/
78 8 ▶ unless ( defined $dlim ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dlim) 8 0 8
79 8 ▶ $dlim = ( $^O eq 'MSWin32' ) ? ':(?!\\/)' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 8 8
80 }
81 8 return split( /$dlim/, $paths );
82 }
83  
84 20 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
85 20 my ( $class, $c, $arguments ) = @_;
86 my $config = {
87 TEMPLATE_EXTENSION => '',
88 20 %{ $class->config },
89 20 %{$arguments},
90 };
91 20 ▶ if ( !( ref $config->{INCLUDE_PATH} eq 'ARRAY' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config->{"INCLUDE_PATH"} eq "ARRAY") 16 4 20
92 16 my $delim = $config->{DELIMITER};
93 16 my @include_path = _coerce_paths( $config->{INCLUDE_PATH}, $delim );
94 16 ▶ if ( !@include_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@include_path) 8 8 16
95 8 my $root = $c->config->{root};
96 8 my $base = Path::Class::dir( $root, 'base' );
97 8 @include_path = ( "$root", "$base" );
98 }
99 16 $config->{INCLUDE_PATH} = \@include_path;
100 }
101  
102 20 ▶ if ( $c->debug && $config->{DUMP_CONFIG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug and $config->{"DUMP_CONFIG"}) 0 20 20
Condition33%1/3
$c->debug and $config->{"DUMP_CONFIG"}
!ll&&!rl&&r
2000
MC/DC0%0/2
$c->debug and $config->{"DUMP_CONFIG"}
$c->debug $config->{"DUMP_CONFIG"}
Truth table33%1/3
A and B
A= $c->debugB= $config->{"DUMP_CONFIG"}
ABresult
0X0
100
111
103 0 $c->log->debug( "TT Config: ", dump($config) );
104 }
105  
106 20 my $self = $class->next::method( $c, {%$config}, );
107  
108 # Set base include paths. Local'd in render if needed
109 20 $self->include_path( $config->{INCLUDE_PATH} );
110  
111 20 $self->config($config);
112  
113 20 return $self;
114 }
115  
116 10 ▶sub process {
Subroutine process: called CC=8 SCAR=29.8
Pod documented
117 10 my ( $self, $c ) = @_;
118  
119 my $template = $c->stash->{template}
120 10 ▶ || $c->action . $self->config->{TEMPLATE_EXTENSION};
Condition66%2/3
$c->stash->{'template'} || $c->action . $self->config->{'TEMPLATE_EXTENSION'}
l!l&&r!l&&!r
640
MC/DC0%0/2
$c->stash->{'template'} || $c->action . $self->config->{'TEMPLATE_EXTENSION'}
$c->stash->{'template'} $c->action . $self->config->{'TEMPLATE_EXTENSION'}
Truth table66%2/3
A || B
A= $c->stash->{'template'}B= $c->action . $self->config->{'TEMPLATE_EXTENSION'}
ABresult
1X1
011
000
121  
122 10 ▶ unless ( defined $template ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $template) 0 10 10
123 0 ▶ $c->log->debug('No template specified for rendering') if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
124 0 return;
125 }
126  
127 10 my $output;
128 10 eval { $output = $self->render( $c, $template ); };
129  
130 10 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 10 10
131 0 my $error_string = qq/Couldn't render template "$template"/;
132  
133 #Mostly copied from Catalyst::View::TT's error handling
134 #Log::Dispatch barfs on ARRAY REF errors
135 0 ▶ if ( blessed($error) && $error->isa('Template::Alloy::Exception') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('Template::Alloy::Exception')) { } 0 0 0
Condition0%0/3
builtin::blessed($error) and $error->isa("Template::Alloy::Exception")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($error) and $error->isa("Template::Alloy::Exception")
builtin::blessed($error) $error->isa("Template::Alloy::Exception")
Truth table0%0/3
A and B
A= builtin::blessed($error)B= $error->isa("Template::Alloy::Exception")
ABresult
0X0
100
111
136 0 $error = "$error_string: $error";
137 0 $c->log->error($error);
138 0 $c->error($error);
139 }
140 else {
141 0 $c->log->error($error);
142 0 $c->error($error);
143 0 return;
144 }
145 }
146  
147 10 ▶ unless ( $c->response->content_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->response->content_type) 10 0 10
148 10 $c->response->content_type('text/html; charset=utf-8');
149 }
150  
151 10 $c->response->body($output);
152  
153 10 return 1;
154 }
155  
156 13 ▶sub render {
Subroutine render: called CC=5 SCAR=16.1
Pod documented
157 13 my ( $self, $c, $template, $args ) = @_;
158  
159 13 ▶ $c->log->debug(qq/Rendering template "$template"/) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 13 13
160  
161 13 my $config = $self->config;
162 13 $config->{INCLUDE_PATH} = $self->include_path;
163  
164 my $vars = {
165 13 ▶ ( ref $args eq 'HASH' ? %$args : %{ $c->stash() } ),
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 2 11 13
166 $self->_template_vars($c)
167 };
168  
169 local $config->{INCLUDE_PATH} =
170 1 [ @{ $vars->{additional_template_paths} }, @{ $config->{INCLUDE_PATH} } ]
171 13 ▶ if ref $vars->{additional_template_paths};
Branch: 1 branch
BranchTrueFalseTotal
if ref $vars->{"additional_template_paths"} 1 12 13
172  
173 # until Template::Alloy either gives us a public method to change
174 # INCLUDE_PATH, or supports a coderef there, we need to create a
175 # new object for every call of render()
176 13 my $tt = Template::Alloy->new($config);
177 13 my $output;
178  
179 13 ▶ unless ( $tt->process( $template, $vars, \$output ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt->process($template, $vars, \$output)) { } 12 1 13
180 1 croak $tt->error;
181 }
182 else {
183 12 return $output;
184 }
185 }
186  
187 13 ▶sub _template_vars {
Subroutine _template_vars: called CC=2 SCAR=7.1
188 13 my ( $self, $c ) = @_;
189  
190 13 my $cvar = $self->config->{CATALYST_VAR};
191  
192 defined $cvar
193 ? ( $cvar => $c )
194 : (
195 c => $c,
196 base => $c->req->base,
197 name => $c->config->{name}
198 13 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $cvar ? : 0 13 13
199 }
200  
201 5 1;
202  
203 __END__
↑ Summary Helper/View/TT/Alloy.pm »