| 1 |
package HTML::FormHandler::Render::WithTT; |
| 2 |
# ABSTRACT: tt rendering |
| 3 |
$HTML::FormHandler::Render::WithTT::VERSION = '0.410002'; |
| 4 |
use Moose::Role; |
| 5 |
use File::ShareDir; |
| 6 |
use Template; |
| 7 |
use namespace::autoclean; |
| 8 |
use HTML::FormHandler::Render::Util ('process_attrs'); |
| 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 |
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 |
my $self = shift; |
| 31 |
return { |
| 32 |
INCLUDE_PATH => [ |
| 33 |
@{ $self->tt_include_path }, |
| 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 |
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 |
my $self = shift; |
| 49 |
|
| 50 |
my $tt_engine = Template->new( $self->tt_config ); |
| 51 |
return $tt_engine; |
| 52 |
} |
| 53 |
|
| 54 |
has 'tt_vars' => ( is => 'rw', traits => ['Hash'], |
| 55 |
builder => 'build_tt_vars'); |
| 56 |
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 |
my $self = shift; |
| 62 |
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 |
sub build_tt_default_options {{}} |
| 73 |
|
| 74 |
|
| 75 |
sub tt_render { |
| 76 |
my $self = shift; |
| 77 |
|
| 78 |
my $output; |
| 79 |
my $vars = { %{$self->default_tt_vars}, %{$self->tt_vars} }; |
| 80 |
$self->tt_engine->process( $self->tt_template, $vars, \$output ); |
| 81 |
|
| 82 |
if( my $exception = $self->tt_engine->{SERVICE}->{_ERROR} ) { |
| 83 |
|
| 84 |
die $exception->[0] . " " . $exception->[1] . ". So far => " . ${$exception->[2]} . "\n"; |
| 85 |
} |
| 86 |
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.410002 |
| 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) 2017 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 |