| 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::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 => '$START_FORM | 
| 29 |  |  |  |  |  |  | $SUBMIT $END_FORM', | 
| 30 |  |  |  |  |  |  | # etc. | 
| 31 |  |  |  |  |  |  | }, | 
| 32 |  |  |  |  |  |  | }, | 
| 33 |  |  |  |  |  |  | ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =cut | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 |  |  | 1 |  | 7 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 81 |  | 
| 38 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 39 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 40 | 1 |  |  | 1 |  | 5 | no  warnings 'uninitialized'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  | 1 |  | 6 | use CGI::FormBuilder::Util; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 168 |  | 
| 43 | 1 |  |  | 1 |  | 638 | use CGI::FastTemplate; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | use base 'CGI::FastTemplate'; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | our $VERSION = '3.09'; | 
| 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__ |