~ubuntu-branches/ubuntu/intrepid/libcgi-formbuilder-perl/intrepid

« back to all changes in this revision

Viewing changes to lib/CGI/FormBuilder/Template/Text.pm

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-28 20:29:04 UTC
  • mfrom: (2.1.7 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080628202904-81kzjon8e8silx88
Tags: 3.05.01-6
Set urgency=medium as 3.05.01-6 included a FTBFS bugfix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
 
2
###########################################################################
 
3
# Copyright (c) 2000-2006 Nate Wiger <nate@wiger.org>. All Rights Reserved.
 
4
# Please visit www.formbuilder.org for tutorials, support, and examples.
 
5
###########################################################################
 
6
 
2
7
package CGI::FormBuilder::Template::Text;
3
8
 
4
9
=head1 NAME
20
25
 
21
26
use Carp;
22
27
use strict;
23
 
 
24
 
our $VERSION = '3.03';
 
28
use warnings;
 
29
no  warnings 'uninitialized';
25
30
 
26
31
use CGI::FormBuilder::Util;
27
32
use Text::Template;
28
33
 
 
34
our $REVISION = do { (my $r='$Revision: 100 $') =~ s/\D+//g; $r };
 
35
our $VERSION = '3.0501';
 
36
 
29
37
sub new {
30
38
    my $self  = shift;
31
39
    my $class = ref($self) || $self;
32
 
    my %opt   = @_;
 
40
    my $opt   = arghash(@_);
33
41
 
34
 
    my $tt_engine = $opt{engine} || {};
 
42
    my $tt_engine = $opt->{engine} || {};
35
43
    unless (UNIVERSAL::isa($tt_engine, 'Text::Template')) {
36
 
        $tt_engine->{&tt_param_name('type',%$tt_engine)}   ||= $opt{TYPE} || 'FILE';
37
 
        $tt_engine->{&tt_param_name('source',%$tt_engine)} ||= $opt{template} || $opt{source} ||
 
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} ||
38
46
            puke "Text::Template source not specified, use the 'template' option";
39
47
        $tt_engine->{&tt_param_name('delimiters',%$tt_engine)} ||= [ '<%','%>' ];
40
 
        $opt{engine} = Text::Template->new(%$tt_engine) || puke $Text::Template::ERROR;
 
48
        $opt->{engine} = Text::Template->new(%$tt_engine) || puke $Text::Template::ERROR;
41
49
    }
42
50
 
43
 
    return bless \%opt, $class;
 
51
    return bless $opt, $class;
44
52
}
45
53
 
46
54
sub engine {
56
64
 
57
65
sub render {
58
66
    my $self = shift;
59
 
    my $form = shift;
60
 
 
61
 
    my %tmplvar = $form->tmpl_param;
62
 
 
63
 
    # Like Template Toolkit, Text::Template can directly access Perl data
64
 
    for my $field ($form->field) {
65
 
 
66
 
        # Extract value since used often
67
 
        my @value = $field->tag_value;
68
 
 
69
 
        # Create a struct for each field
70
 
        $tmplvar{field}{"$field"} = {
71
 
             %$field,
72
 
             field   => $field->tag,
73
 
             label   => $field->label,
74
 
             value   => $value[0],
75
 
             values  => \@value,
76
 
             options => [$field->options],
77
 
             type    => $field->type,
78
 
             comment => $field->comment,
79
 
        };
80
 
        $tmplvar{field}{"$field"}{error} = $field->error;
81
 
    }
 
67
    my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)";
82
68
 
83
69
    my $tt_data;
84
70
    if (ref $self->{data} eq 'ARRAY') {
86
72
    } else {
87
73
        $tt_data = [ $self->{data} ];
88
74
    }
89
 
 
90
75
    my $tt_var  = $self->{variable};      # optional var for nesting
91
76
 
92
 
    # must generate JS first because it affects the others
93
 
    $tmplvar{'jshead'} = $form->script;
94
 
    $tmplvar{'title'}  = $form->title;
95
 
    $tmplvar{'start'}  = $form->start . $form->statetags . $form->keepextras;
96
 
    $tmplvar{'submit'} = $form->submit;
97
 
    $tmplvar{'reset'}  = $form->reset;
98
 
    $tmplvar{'end'}    = $form->end;
99
 
    $tmplvar{'invalid'}= $form->invalid;
100
 
    $tmplvar{'fields'} = [ map $tmplvar{field}{$_}, $form->field ];
101
77
    if ($tt_var) {
102
 
        push @$tt_data, { $tt_var => \%tmplvar };
 
78
        push @$tt_data, { $tt_var => $tvar };
103
79
    } else {
104
 
        push @$tt_data, \%tmplvar;
 
80
        push @$tt_data, $tvar;
105
81
    }
106
82
 
107
83
    my $tt_fill_in = $self->{fill_in} || {};
114
90
 
115
91
    $tt_fill_in_hash = {} unless @$tt_fill_in_hash;
116
92
    $tt_fill_in->{&tt_param_name('hash',%$tt_fill_in)} = $tt_fill_in_hash;
 
93
 
117
94
    my $tt_output = $self->{engine}->fill_in(%$tt_fill_in)
118
95
        || puke "Text::Template expansion failed: $Text::Template::ERROR";
119
96
 
120
 
    return $form->header . $tt_output;
 
97
    return $tt_output;
121
98
}
122
99
 
123
100
1;
149
126
about 25%, and the C<< <% >> and C<< %> >> delimiters are good,
150
127
familiar-looking alternatives).
151
128
 
 
129
The following methods are provided (usually only used internally):
 
130
 
 
131
=head2 engine
 
132
 
 
133
Returns a reference to the C<Text::Template> object
 
134
 
 
135
=head2 prepare
 
136
 
 
137
Returns a hash of all the fields ready to be rendered.
 
138
 
 
139
=head2 render
 
140
 
 
141
Uses the prepared hash and expands the template, returning a string of HTML.
 
142
 
 
143
=head1 TEMPLATES
 
144
 
152
145
    <% $jshead %>  -  JavaScript to stick in <head>
153
146
    <% $title  %>  -  The <title> of the HTML form
154
147
    <% $start  %>  -  Opening <form> tag and internal fields
291
284
 
292
285
=head1 REVISION
293
286
 
294
 
$Id: Text.pm,v 1.32 2006/02/24 01:42:29 nwiger Exp $
 
287
$Id: Text.pm 100 2007-03-02 18:13:13Z nwiger $
295
288
 
296
289
=head1 AUTHOR
297
290