File Coverage

blib/lib/HTML/FormHandler/Render/WithTT.pm
Criterion Covered Total %
statement 32 37 86.4
branch 1 2 50.0
condition n/a
subroutine 10 13 76.9
pod 0 8 0.0
total 43 60 71.6


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Render::WithTT;
2             # ABSTRACT: tt rendering
3             $HTML::FormHandler::Render::WithTT::VERSION = '0.40067';
4 4     4   3725 use Moose::Role;
  4         8  
  4         38  
5 4     4   15625 use File::ShareDir;
  4         6  
  4         165  
6 4     4   15 use Template;
  4         4  
  4         97  
7 4     4   14 use namespace::autoclean;
  4         5  
  4         36  
8 4     4   1433 use HTML::FormHandler::Render::Util ('process_attrs');
  4         7  
  4         21  
9              
10              
11             has 'tt_include_path' => (
12             traits => ['Array'],
13             is => 'rw',
14             isa => 'ArrayRef',
15             lazy => 1,
16             builder => 'build_tt_include_path',
17             handles => {
18             add_tt_include_path => 'push',
19             }
20             );
21 0     0 0 0 sub build_tt_include_path {[]}
22              
23             has 'tt_config' => (
24             traits => ['Hash'],
25             is => 'rw',
26             lazy => 1,
27             builder => 'build_tt_config',
28             );
29             sub build_tt_config {
30 5     5 0 10 my $self = shift;
31             return {
32             INCLUDE_PATH => [
33 5         9 @{ $self->tt_include_path },
  5         125  
34             File::ShareDir::dist_dir('HTML-FormHandler') . '/templates/'
35             ]
36             };
37             }
38              
39             # either file name string or string ref?
40             has 'tt_template' => ( is => 'rw', isa => 'Str', lazy => 1,
41             builder => 'build_tt_template' );
42 0     0 0 0 sub build_tt_template { 'form/form.tt' }
43              
44             has 'tt_engine' => ( is => 'rw', isa => 'Template', lazy => 1,
45             builder => 'build_tt_engine'
46             );
47             sub build_tt_engine {
48 5     5 0 9 my $self = shift;
49              
50 5         126 my $tt_engine = Template->new( $self->tt_config );
51 5         69674 return $tt_engine;
52             }
53              
54             has 'tt_vars' => ( is => 'rw', traits => ['Hash'],
55             builder => 'build_tt_vars');
56 5     5 0 3739 sub build_tt_vars {{}}
57              
58             has 'default_tt_vars' => ( is => 'ro', isa => 'HashRef',
59             lazy => 1, builder => 'build_default_tt_vars' );
60             sub build_default_tt_vars {
61 5     5 0 9 my $self = shift;
62 5         26 return { form => $self->form, process_attrs => \&process_attrs };
63             }
64              
65             has 'tt_default_options' => (
66             traits => ['Hash'],
67             is => 'rw',
68             isa => 'HashRef',
69             lazy => 1,
70             builder => 'build_tt_default_options',
71             );
72 0     0 0 0 sub build_tt_default_options {{}}
73              
74              
75             sub tt_render {
76 5     5 0 488 my $self = shift;
77              
78 5         10 my $output;
79 5         6 my $vars = { %{$self->default_tt_vars}, %{$self->tt_vars} };
  5         158  
  5         119  
80 5         125 $self->tt_engine->process( $self->tt_template, $vars, \$output );
81              
82 5 50       528 if( my $exception = $self->tt_engine->{SERVICE}->{_ERROR} ) {
83              
84 0         0 die $exception->[0] . " " . $exception->[1] . ". So far => " . ${$exception->[2]} . "\n";
  0         0  
85             }
86 5         28 return $output;
87             }
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             HTML::FormHandler::Render::WithTT - tt rendering
100              
101             =head1 VERSION
102              
103             version 0.40067
104              
105             =head1 SYNOPSIS
106              
107             A rendering role for HTML::FormHandler that allows rendering using
108             Template::Toolkit
109              
110             package MyApp::Form;
111             use HTML::FormHandler::Moose;
112             extends 'HTML::FormHandler';
113             with 'HTML::FormHandler::Render::WithTT';
114              
115             sub build_tt_template { 'user_form.tt' }
116             sub build_tt_include_path { ['root/templates'] }
117             ....< define form >....
118              
119             my $form = MyApp::Form->new(
120             $form->tt_render;
121              
122             If you want to render with TT, you don't need this role. Just use
123             one of the TT form templates provided, form.tt or form_in_one.tt.
124             If you use this role to render, you are using two different TT
125             engines, with different sets of variables, etc, which doesn't
126             make much sense.
127              
128             This is mainly useful as a testing aid and an example of using the
129             sample templates.
130              
131             =head1 DESCRIPTION
132              
133             Uses 'tt_render' instead of 'render' to allow using both TT templates and the
134             built-in rendering.
135              
136             =head1 AUTHOR
137              
138             FormHandler Contributors - see HTML::FormHandler
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2016 by Gerda Shank.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut