WebNano/Renderer/TTiny.pm

filter: Statementstmt 97.2% 70 / 72 Branchbran 75.0% 21 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 92.1% 105 / 114 CC 19 SCAR 24.7
CC
19
Cov
91%
render
17.2
_to_list
3.1
BEGIN
1.0
CRAP 11.8
↑ Summary WebNano/Controller.pm »
1 package WebNano::Renderer::TTiny;
2 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 $WebNano::Renderer::TTiny::VERSION = '0.007';
4 }
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Template::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Object::Tiny::RW qw/ root _tt_tiny INCLUDE_PATH TEMPLATE_EXTENSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 29 ▶sub _to_list {
Subroutine _to_list: called CC=3 SCAR=11.3
14 29 ▶ if( ref $_[0] ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 13 16 29
elsif (not defined $_[0]) { } 0 16 16
15 13 return @{ $_[0] };
16 }
17 elsif( ! defined $_[0] ){
18 0 return ();
19 }
20 else{
21 16 return $_[0];
22 }
23 }
24  
25 23 ▶sub render {
Subroutine render: called CC=17 SCAR=28.5
Pod documented
26 23 my( $self, %vars ) = @_;
27 23 my $c = $vars{c};
28  
29 23 my @search_path;
30 23 ▶ if( $c ){
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 23 0 23
31 23 my $path = ref $c;
32 23 $path =~ s/.*::Controller(::)?//;
33 23 $path =~ s{::}{/};
34 23 @search_path = ( $path, @{ $c->template_search_path });
35 }
36 23 ▶ if( !@search_path ){
Branch: 1 branch
BranchTrueFalseTotal
unless (@search_path) 0 23 23
37 0 @search_path = ( '' );
38 }
39 23 my $template = $vars{template};
40 23 ▶ if( !$template ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($template) 2 21 23
41 2 my @caller = caller(2);
42 2 $template = $caller[3];
43 2 $template =~ s/_action$//;
44 2 $template =~ s/^.*:://;
45 2 ▶ $template .= '.' . $self->TEMPLATE_EXTENSION if $self->TEMPLATE_EXTENSION;
Branch: 1 branch
BranchTrueFalseTotal
if $self->TEMPLATE_EXTENSION 0 2 2
46 }
47 23 my $full_template;
48 LOOP:
49 23 for my $path ( @search_path ){
50 34 my $to_check;
51 34 ▶ if( !$self->root || File::Spec->file_name_is_absolute( $path ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->root or 'File::Spec'->file_name_is_absolute($path)) { } 6 28 34
Condition100%3/3
not $self->root or "File::Spec"->file_name_is_absolute($path)
l!l&&r!l&&!r
2428
MC/DC100%2/2
not $self->root or "File::Spec"->file_name_is_absolute($path)
not $self->root "File::Spec"->file_name_is_absolute($path)
Truth table100%3/3
not(A) or B
A= not $self->rootB= "File::Spec"->file_name_is_absolute($path)
ABresult
1X1
011
000
52 6 $to_check = File::Spec->catfile( $path, $template );
53 6 ▶ if( -f $to_check ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $to_check) 4 2 6
54 4 $full_template = $to_check;
55 4 last LOOP;
56 }
57 }
58 else{
59 28 for my $root ( _to_list( $self->root ) ){
60 40 $to_check = File::Spec->catfile( $root, $path, $template );
61 40 ▶ if( -f $to_check ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $to_check) 18 22 40
62 18 $full_template = $to_check;
63 18 last LOOP;
64 }
65 }
66 }
67 }
68 23 my @static_search_path;
69 23 ▶ if( !$full_template ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($full_template) 1 22 23
70 1 @static_search_path = _to_list( $self->INCLUDE_PATH );
71 STATIC_LOOP:
72 1 for my $path ( @static_search_path ){
73 1 my $to_check;
74 1 $to_check = File::Spec->catfile( $path, $template );
75 1 ▶ if( -f $to_check ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $to_check) 1 0 1
76 1 $full_template = $to_check;
77 1 last STATIC_LOOP;
78 }
79 }
80 }
81 23 ▶ die "Cannot find $template in search path: @search_path, @static_search_path" if !defined $full_template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $full_template 0 23 23
82 23 ▶ open my $fh, $full_template or die "Cannot read from $full_template: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $full_template or die "Cannot read from ${full_template}: $!" 23 0 23
83 23 my $string = do { local $/; <$fh> };
84 23 ▶ if( !$self->_tt_tiny ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_tt_tiny) 5 18 23
85 5 $self->_tt_tiny( Template::Tiny->new() );
86 }
87 23 my $out;
88 23 $self->_tt_tiny->process( \$string, \%vars, \$out );
89 23 return $out;
90 }
91  
92 3 1;
93  
94  
95  
96 =pod
97  
98 =head1 NAME
99  
100 WebNano::Renderer::TTiny - Dynamic search paths for Template::Tiny
101  
102 =head1 VERSION
103  
104 version 0.007
105  
106 =head1 SYNOPSIS
107  
108 in MyApp.pm:
109  
110 $self->renderer(
111 WebNano::Renderer::TTiny->new( root => 't/data/templates' )
112 );
113  
114 in MyApp::Controller:
115  
116 return $self->render( template => 'some_template.tt', some_var => 'some_value );
117  
118 =head1 DESCRIPTION
119  
120 This is a wrapper around L<Template::Tiny>
121 - 'Template Toolkit reimplemented in as little code as possible'.
122  
123 The only public method here is render - it expects as input a hash with the
124 following data:
125  
126 =over
127  
128 =item template - the name of the template file
129  
130 =item c - the controller
131  
132 =back
133  
134 The template is then looked for in the directories in C<INCLUDE_PATH> and in
135 directories constructed dynamically from the paths in C<root> and the controller
136 name. For example if 'root' contains C<[ 'template', 'additional_templates' ]>
137 and the controller name is C<MyApp::Controller::SubController> then the template
138 will be looked for in C<template/SubController> and
139 C<additional_templates/SubController>. This mechanism is designed so that it is
140 possible for a way of subclassing templates along with subclassing controllers.
141 If this is too complicated - you can provide no value for the C<root> attribute
142 and use only C<INCLUDE_PATH>.
143  
144 When the template is found - the C<process> method on the internal
145 C<Template::Tiny> object is called. A reference to the whole hash passed to
146 C<render> is passed to the C<process> call - so that all the values are
147 available in the template itself.
148  
149 If no template name is passed - then it is guessed from the name of the
150 controller method that called C<render> (this is done using L<caller>) and the
151 C<TEMPLATE_EXTENSION> attribute.
152  
153 =head1 ATTRIBUTES and METHODS
154  
155 =head2 render
156  
157 =head2 INCLUDE_PATH
158  
159 Static list of template search directories.
160  
161 =head2 root
162  
163 List of directories that are dynamically concatenated with controller names to form
164 a dynamic search list of template directories.
165  
166 You can use INCLUDE_PATH or root or both.
167  
168 =head2 TEMPLATE_EXTENSION
169  
170 Postfix added to action name to form the template name ( for example 'edit.tt'
171 from action 'edit' and TEMPLATE_EXTENSION 'tt' ).
172  
173 =head1 AUTHOR
174  
175 Zbigniew Lukasiak <zby@cpan.org>
176  
177 =head1 COPYRIGHT AND LICENSE
178  
179 This software is Copyright (c) 2010 by Zbigniew Lukasiak <zby@cpan.org>.
180  
181 This is free software, licensed under:
182  
183 The Artistic License 2.0 (GPL Compatible)
184  
185 =cut
186  
187  
188 __END__
↑ Summary WebNano/Controller.pm »