File: WithTT.pm

package info (click to toggle)
libhtml-formhandler-perl 0.40057-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,320 kB
  • ctags: 685
  • sloc: perl: 8,849; makefile: 2
file content (147 lines) | stat: -rw-r--r-- 3,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package HTML::FormHandler::Render::WithTT;
# ABSTRACT: tt rendering

use Moose::Role;
use File::ShareDir;
use Template;
use namespace::autoclean;
use HTML::FormHandler::Render::Util ('process_attrs');


has 'tt_include_path' => (
    traits => ['Array'],
    is => 'rw',
    isa => 'ArrayRef',
    lazy => 1,
    builder => 'build_tt_include_path',
    handles => {
       add_tt_include_path => 'push',
    }
);
sub build_tt_include_path {[]}

has 'tt_config' => (
    traits => ['Hash'],
    is => 'rw',
    lazy => 1,
    builder => 'build_tt_config',
);
sub build_tt_config {
    my $self = shift;
    return {
        INCLUDE_PATH => [
           @{ $self->tt_include_path },
           File::ShareDir::dist_dir('HTML-FormHandler') . '/templates/'
        ]
    };
}

# either file name string or string ref?
has 'tt_template' => ( is => 'rw', isa => 'Str', lazy => 1,
   builder => 'build_tt_template' );
sub build_tt_template { 'form/form.tt' }

has 'tt_engine' => ( is => 'rw', isa => 'Template', lazy => 1,
   builder => 'build_tt_engine'
);
sub build_tt_engine {
    my $self = shift;

    my $tt_engine = Template->new( $self->tt_config );
    return $tt_engine;
}

has 'tt_vars' => ( is => 'rw', traits => ['Hash'],
    builder => 'build_tt_vars');
sub build_tt_vars {{}}

has 'default_tt_vars' => ( is => 'ro', isa => 'HashRef',
   lazy => 1, builder => 'build_default_tt_vars' );
sub build_default_tt_vars {
    my $self = shift;
    return { form => $self->form, process_attrs => \&process_attrs };
}

has 'tt_default_options' => (
    traits => ['Hash'],
    is => 'rw',
    isa => 'HashRef',
    lazy => 1,
    builder => 'build_tt_default_options',
);
sub build_tt_default_options {{}}


sub tt_render {
    my $self = shift;

    my $output;
    my $vars = { %{$self->default_tt_vars}, %{$self->tt_vars} };
    $self->tt_engine->process( $self->tt_template, $vars, \$output );

    if( my $exception = $self->tt_engine->{SERVICE}->{_ERROR} ) {

        die $exception->[0] . " " . $exception->[1] . ".  So far => " . ${$exception->[2]} . "\n";
    }
    return $output;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

HTML::FormHandler::Render::WithTT - tt rendering

=head1 VERSION

version 0.40057

=head1 SYNOPSIS

A rendering role for HTML::FormHandler that allows rendering using
Template::Toolkit

   package MyApp::Form;
   use HTML::FormHandler::Moose;
   extends 'HTML::FormHandler';
   with 'HTML::FormHandler::Render::WithTT';

   sub build_tt_template { 'user_form.tt' }
   sub build_tt_include_path { ['root/templates'] }
   ....< define form >....

   my $form = MyApp::Form->new(
   $form->tt_render;

If you want to render with TT, you don't need this role. Just use
one of the TT form templates provided, form.tt or form_in_one.tt.
If you use this role to render, you are using two different TT
engines, with different sets of variables, etc, which doesn't
make much sense.

This is mainly useful as a testing aid and an example of using the
sample templates.

=head1 DESCRIPTION

Uses 'tt_render' instead of 'render' to allow using both TT templates and the
built-in rendering.

=head1 AUTHOR

FormHandler Contributors - see HTML::FormHandler

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Gerda Shank.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut