File Coverage

blib/lib/App/AutoCRUD/View/TT.pm
Criterion Covered Total %
statement 37 37 100.0
branch 2 4 50.0
condition 1 2 50.0
subroutine 11 11 100.0
pod 0 3 0.0
total 51 57 89.4


line stmt bran cond sub pod time code
1             package App::AutoCRUD::View::TT;
2              
3 2     2   944 use 5.010;
  2         6  
4 2     2   7 use strict;
  2         2  
  2         35  
5 2     2   6 use warnings;
  2         2  
  2         47  
6              
7 2     2   7 use Moose;
  2         2  
  2         11  
8             extends 'App::AutoCRUD::View';
9              
10 2     2   9499 use Template;
  2         27260  
  2         48  
11 2     2   927 use Template::Filters ();
  2         7306  
  2         40  
12 2     2   10 use Encode qw/encode_utf8/;
  2         2  
  2         154  
13              
14 2     2   9 use namespace::clean -except => 'meta';
  2         3  
  2         16  
15              
16             has 'tt_args' => ( is => 'bare', isa => 'HashRef', default => sub {{}} );
17              
18              
19             sub render {
20 10     10 0 16 my ($self, $data, $context) = @_;
21              
22             # where to find templates
23 10         242 my @dirs = map {"$_/templates"} $context->app->share_paths;
  10         38  
24 10         45 unshift @dirs, $context->dir . "/templates";
25              
26             my %tt_args = (
27             INCLUDE_PATH => \@dirs,
28             PRE_PROCESS => 'lib/config',
29             WRAPPER => 'lib/site/wrapper',
30             ERROR => 'src/error.tt',
31             ENCODING => 'utf8',
32             FILTERS => { utf8_url => \&utf8_url },
33 10         36 %{$self->{tt_args}},
  10         68  
34             );
35              
36 10         78 my $renderer = Template->new(%tt_args);
37 10 50       27610 my $template = $context->template
38             or die "no template for TT view";
39 10 50       71 $renderer->process("src/$template",
40             {data => $data, c => $context},
41             \my $output)
42             or die $renderer->error;
43              
44 10         788 return [200, ['Content-type' => 'text/html; charset=utf-8',
45             'X-UA-Compatible' => "IE=edge", # enforce latest MSIE rendering
46             ],
47             [encode_utf8($output)] ];
48             }
49              
50              
51             sub default_dashed_args {
52 3     3 0 6 my ($self, $context) = @_;
53              
54 3   50     87 return (-page_index => 1,
55             -page_size => ($context->app->default('page_size') || 50));
56             }
57              
58              
59             sub utf8_url {
60 72     72 0 83 my $data = shift;
61 72         140 return Template::Filters::url_filter(encode_utf8($data));
62             }
63              
64              
65              
66              
67             1;
68              
69              
70             __END__
71              
72              
73             # code partially borrowed from Catalyst::Helper::View::TTSite