lib/CGI/FormBuilder/Template/Fast.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 SCAR 0.0
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/CGI/FormBuilder/Template/CGI_SSI.pm ↑ Summary lib/CGI/FormBuilder/Template/HTML.pm »
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 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use CGI::FastTemplate;
Subroutine BEGIN: called CC=1 SCAR=2.6
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__
« lib/CGI/FormBuilder/Template/CGI_SSI.pm ↑ Summary lib/CGI/FormBuilder/Template/HTML.pm »