line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
########################################################################### |
3
|
|
|
|
|
|
|
# Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved. |
4
|
|
|
|
|
|
|
# Please visit http://formbuilder.org for tutorials, support, and examples. |
5
|
|
|
|
|
|
|
########################################################################### |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package CGI::FormBuilder::Template::Text; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
CGI::FormBuilder::Template::Text - FormBuilder interface to Text::Template |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $form = CGI::FormBuilder->new( |
16
|
|
|
|
|
|
|
fields => \@fields, |
17
|
|
|
|
|
|
|
template => { |
18
|
|
|
|
|
|
|
type => 'Text', |
19
|
|
|
|
|
|
|
template => 'form.tmpl', |
20
|
|
|
|
|
|
|
variable => 'form', |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
69
|
|
27
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
28
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
29
|
1
|
|
|
1
|
|
5
|
no warnings 'uninitialized'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
1
|
|
5
|
use CGI::FormBuilder::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
152
|
|
32
|
1
|
|
|
1
|
|
479
|
use Text::Template; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $VERSION = '3.09'; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub new { |
38
|
|
|
|
|
|
|
my $self = shift; |
39
|
|
|
|
|
|
|
my $class = ref($self) || $self; |
40
|
|
|
|
|
|
|
my $opt = arghash(@_); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $tt_engine = $opt->{engine} || {}; |
43
|
|
|
|
|
|
|
unless (UNIVERSAL::isa($tt_engine, 'Text::Template')) { |
44
|
|
|
|
|
|
|
$tt_engine->{&tt_param_name('type',%$tt_engine)} ||= $opt->{TYPE} || 'FILE'; |
45
|
|
|
|
|
|
|
$tt_engine->{&tt_param_name('source',%$tt_engine)} ||= $opt->{template} || $opt->{source} || |
46
|
|
|
|
|
|
|
puke "Text::Template source not specified, use the 'template' option"; |
47
|
|
|
|
|
|
|
$tt_engine->{&tt_param_name('delimiters',%$tt_engine)} ||= [ '<%','%>' ]; |
48
|
|
|
|
|
|
|
$opt->{engine} = Text::Template->new(%$tt_engine) || puke $Text::Template::ERROR; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
return bless $opt, $class; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub engine { |
55
|
|
|
|
|
|
|
return shift()->{engine}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# This sub helps us to support all of Text::Template's argument naming conventions |
59
|
|
|
|
|
|
|
sub tt_param_name { |
60
|
|
|
|
|
|
|
my ($arg, %h) = @_; |
61
|
|
|
|
|
|
|
my ($key) = grep { exists $h{$_} } ($arg, "\u$arg", "\U$arg", "-$arg", "-\u$arg", "-\U$arg"); |
62
|
|
|
|
|
|
|
return $key || $arg; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub render { |
66
|
|
|
|
|
|
|
my $self = shift; |
67
|
|
|
|
|
|
|
my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)"; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $tt_data; |
70
|
|
|
|
|
|
|
if (ref $self->{data} eq 'ARRAY') { |
71
|
|
|
|
|
|
|
$tt_data = $self->{data}; |
72
|
|
|
|
|
|
|
} else { |
73
|
|
|
|
|
|
|
$tt_data = [ $self->{data} ]; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
my $tt_var = $self->{variable}; # optional var for nesting |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
if ($tt_var) { |
78
|
|
|
|
|
|
|
push @$tt_data, { $tt_var => $tvar }; |
79
|
|
|
|
|
|
|
} else { |
80
|
|
|
|
|
|
|
push @$tt_data, $tvar; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $tt_fill_in = $self->{fill_in} || {}; |
84
|
|
|
|
|
|
|
my $tt_fill_in_hash = $tt_fill_in->{&tt_param_name('hash',%$tt_fill_in)} || {}; |
85
|
|
|
|
|
|
|
if (ref($tt_fill_in_hash) eq 'ARRAY') { |
86
|
|
|
|
|
|
|
push @$tt_fill_in_hash, @$tt_data; |
87
|
|
|
|
|
|
|
} else { |
88
|
|
|
|
|
|
|
$tt_fill_in_hash = [ $tt_fill_in_hash, @$tt_data ]; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
$tt_fill_in_hash = {} unless @$tt_fill_in_hash; |
92
|
|
|
|
|
|
|
$tt_fill_in->{&tt_param_name('hash',%$tt_fill_in)} = $tt_fill_in_hash; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $tt_output = $self->{engine}->fill_in(%$tt_fill_in) |
95
|
|
|
|
|
|
|
|| puke "Text::Template expansion failed: $Text::Template::ERROR"; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
return $tt_output; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
__END__ |