blib/lib/CGI/FormBuilder/Template/Fast.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 75 SCAR 24.8
CC
3
Cov
0%
prepare
12.0
new
2.0
engine
2.0
CRAP 12.0
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::Fast;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Template::Fast - FormBuilder interface to CGI::FastTemplate
12  
13 =head1 SYNOPSIS
14  
15 my $form = CGI::FormBuilder->new(
16 fields => \@whatever,
17 template => {
18 type => 'Fast',
19 root => '/path/to/templates',
20 # use external files
21 define => {
22 form => 'form.txt',
23 field => 'field.txt',
24 invalid_field => 'invalid_field.txt',
25 },
26 # or define inline
27 define_nofile => {
28 form => '<html><head></head><body>$START_FORM
29 <table>$FIELDS</table>$SUBMIT $END_FORM</body></html>',
30 # etc.
31 },
32 },
33 );
34  
35 =cut
36  
37 use Carp;
38 use strict;
39 use warnings;
40 no warnings 'uninitialized';
41  
42 use CGI::FormBuilder::Util;
43 use CGI::FastTemplate;
44 use base 'CGI::FastTemplate';
45  
46  
47 our $VERSION = '3.10';
48  
49 sub new {
50 my $self = shift;
51 my $class = ref($self) || $self;
52 my $opt = arghash(@_);
53  
54 my $t = CGI::FastTemplate->new($opt->{root});
55  
56 # turn off strict so that undef vars show up
57 # as blank in the template output
58 $t->no_strict;
59  
60 # define our templates
61 $t->define(%{ $opt->{define} });
62 $t->define_raw($opt->{define_raw});
63 $t->define_nofile($opt->{define_nofile});
64  
65 # jam $t info FB 'engine' container
66 $opt->{engine} = $t;
67  
68 return bless $opt, $class;
69 }
70  
71 sub engine {
72 return shift()->{engine};
73 }
74  
75 sub prepare {
76 my $self = shift;
77 my $form = shift;
78  
79 # a couple special fields
80 my %tmplvar = $form->tmpl_param;
81  
82 # Go over the fields and process each one
83 for my $field ($form->field) {
84 # Extract value since used often
85 my @value = $field->tag_value;
86  
87 # assign this field's variables
88 my $ref = {
89 NAME => $field->name,
90 FIELD => $field->tag,
91 VALUE => $value[0], # the VALUE tag can only hold first value!
92 LABEL => $field->label,
93 REQUIRED => ($field->required ? 'required' : 'optional'),
94 ERROR => $field->error,
95 COMMENT => $field->comment,
96 };
97 $self->{engine}->assign($ref);
98  
99 # TODO: look for special templates based on field name or type?
100 if ($field->invalid) {
101 $self->{engine}->parse(FIELDS => '.invalid_field');
102 } else {
103 $self->{engine}->parse(FIELDS => '.field');
104 }
105  
106 $self->{engine}->clear_href;
107 }
108  
109 # a couple special fields
110 $self->{engine}->assign({
111 TITLE => $form->title,
112 JS_HEAD => $form->script,
113 START_FORM => $form->start . $form->statetags . $form->keepextras,
114 SUBMIT => $form->submit,
115 RESET => $form->reset,
116 END_FORM => $form->end,
117 %tmplvar,
118 });
119 $self->{engine}->parse(FORM => 'form');
120  
121 return $self;
122 }
123  
124 sub render {
125 my $self = shift;
126 return ${ $self->{engine}->fetch('FORM') };
127 }
128  
129  
130 # End of Perl code
131 1;
132 __END__