File Coverage

blib/lib/Plack/App/TemplateToolkit.pm
Criterion Covered Total %
statement 49 52 94.2
branch 17 28 60.7
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 78 92 84.7


line stmt bran cond sub pod time code
1             package Plack::App::TemplateToolkit;
2             BEGIN {
3 1     1   52889 $Plack::App::TemplateToolkit::VERSION = '0.06';
4             }
5 1     1   17 use strict;
  1         2  
  1         26  
6 1     1   5 use warnings;
  1         2  
  1         24  
7              
8 1     1   5 use parent qw( Plack::Component );
  1         2  
  1         4  
9 1     1   854 use Plack::Request 0.9901;
  1         205354  
  1         30  
10 1     1   2413 use Template 2;
  1         32007  
  1         51  
11              
12             use Plack::Util::Accessor
13 1     1   9 qw( root interpolate post_chomp dir_index path extension content_type tt eval_perl pre_process process);
  1         2  
  1         13  
14              
15             sub prepare_app {
16 3     3 1 4836 my ($self) = @_;
17              
18 3 50       18 die "No root supplied" unless $self->root();
19              
20 3 50       95 $self->dir_index('index.html') unless $self->dir_index();
21 3 50       52 $self->content_type('text/html') unless $self->content_type();
22 3 50       44 $self->interpolate(0) unless defined $self->interpolate();
23 3 50       44 $self->eval_perl(0) unless defined $self->eval_perl();
24 3 50       42 $self->post_chomp(1) unless defined $self->post_chomp();
25              
26 3         41 my $config = {
27             INCLUDE_PATH => $self->root(), # or list ref
28             INTERPOLATE => $self->interpolate(), # expand "$var" in plain text
29             POST_CHOMP => $self->post_chomp(), # cleanup whitespace
30             EVAL_PERL => $self->eval_perl(), # evaluate Perl code blocks
31             };
32              
33 3 100       49 $config->{PRE_PROCESS} = $self->pre_process() if $self->pre_process();
34 3 100       32 $config->{PROCESS} = $self->process() if $self->process();
35              
36             # create Template object
37 3         49 $self->tt( Template->new($config) );
38              
39             }
40              
41             sub call {
42 5     5 1 117109 my $self = shift;
43 5         9 my $env = shift;
44              
45 5 50       13 if ( my $res = $self->_handle_tt($env) ) {
46 5         100 return $res;
47             }
48 0         0 return [ 404, [ 'Content-Type' => 'text/html' ], ['404 Not Found'] ];
49             }
50              
51             sub _handle_tt {
52 5     5   10 my ( $self, $env ) = @_;
53              
54 5         10 my $path = $env->{PATH_INFO};
55              
56 5 100       38 if ( $path !~ /\.\w{1,6}$/ ) {
57              
58             # Use this regex instead of -e as $self->root can be a list ref
59             # TT will sort it out,
60              
61             # No file extension
62 1         5 $path .= $self->dir_index;
63             }
64              
65 5 50       25 if ( my $extension = $self->extension() ) {
66 0 0       0 return 0 unless $path =~ /${extension}$/;
67             }
68              
69 5         40 my $tt = $self->tt();
70              
71 5         45 my $req = Plack::Request->new($env);
72              
73 5         57 my $vars = { params => $req->query_parameters(), };
74              
75 5         272 my $content;
76 5         22 $path =~ s{^/}{}; # Do not want to enable absolute paths
77              
78 5 100       26 if ( $tt->process( $path, $vars, \$content ) ) {
79             return [
80 4         87043 '200', [ 'Content-Type' => $self->content_type() ],
81             [$content]
82             ];
83             } else {
84 1         272 my $error = $tt->error->as_string();
85 1 50       16 if ( $error =~ /not found/ ) {
86             return [
87 1         6 '404', [ 'Content-Type' => $self->content_type() ],
88             [$error]
89             ];
90             } else {
91             return [
92 0           '500', [ 'Content-Type' => $self->content_type() ],
93             [$error]
94             ];
95             }
96             }
97             }
98              
99             1;
100              
101             # ABSTRACT: DEPRECIATED use Plack::Middleware::TemplateToolkit
102              
103              
104              
105             =pod
106              
107             =head1 NAME
108              
109             Plack::App::TemplateToolkit - DEPRECIATED use Plack::Middleware::TemplateToolkit
110              
111             =head1 VERSION
112              
113             version 0.06
114              
115             =head1 SYNOPSIS
116              
117             Use Plack::Middleware::TemplateToolkit instead of this.
118              
119             =head1 DESCRIPTION
120              
121             Use Plack::Middleware::TemplateToolkit instead of this package.
122              
123             =head1 NAME
124              
125             Plack::App::TemplateToolkit DEPRECIATED use Plack::Middleware::TemplateToolkit
126              
127             =head1 SEE ALSO
128              
129             L
130              
131             =head1 AUTHOR
132              
133             Leo Lapworth
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             This software is copyright (c) 2011 by Leo Lapworth.
138              
139             This is free software; you can redistribute it and/or modify it under
140             the same terms as the Perl 5 programming language system itself.
141              
142             =cut
143              
144              
145             __END__