Changeset 34 in text-formbuilder


Ignore:
Timestamp:
11/19/04 09:44:42 (20 years ago)
Author:
peter
Message:

customizable messages similar to CGI::FormBuilder
customizable charset for the generated HTML page

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Changes

    r32 r34  
    55    * added a single-quoted string to the grammar that 
    66      can be used in the labels and default values to include 
    7       characters in [^\w\t ] 
     7      characters not in [\w\t ] 
    88    * generated code leaves out overwrriten options 
    99    * allow option lists to have simple multiword and quoted 
     
    1414    * fall through to CGI::FormBuilder builtin option lists 
    1515      if @LIST does not match a list directive 
     16    * customizable messages similar to CGI::FormBuilder 
     17    * customizable charset for the generated HTML page 
    1618     
    17190.05 -  9 Nov 2004 
  • trunk/lib/Text/FormBuilder.pm

    r33 r34  
    3232END 
    3333 
     34# default messages that can be localized 
    3435my %DEFAULT_MESSAGES = ( 
    35     text_formbuilder_created => 'Created by %s', 
    36     text_formbuilder_madewith => 'Made with %s', 
    37     text_formbuilder_required => 'Required fields are marked in <strong>bold</strong>.', 
     36    text_author   => 'Created by %s', 
     37    text_madewith => 'Made with %s version %s', 
     38    text_required => '(Required fields are marked in <strong>bold</strong>.)', 
     39    text_invalid  => 'Missing or invalid value.', 
    3840); 
     41 
     42my $DEFAULT_CHARSET = 'iso-8859-1'; 
    3943 
    4044sub new { 
     
    98102    #   neat trick: extra_css => '@import(my_external_stylesheet.css);' 
    99103    #   will let you use an external stylesheet 
     104    #   CSS Hint: to get multiple sections to all line up their fields, 
     105    #   set a standard width for th.label 
    100106    my $css; 
    101107    $css = $options{css} || $DEFAULT_CSS; 
     
    115121            # filename, just *warn* on missing, and use defaults 
    116122            if (-f $options{messages} && -r _ && open(MESSAGES, "< $options{messages}")) { 
    117                 $options{messages} = {}; 
     123                $options{messages} = { %DEFAULT_MESSAGES }; 
    118124                while(<MESSAGES>) { 
    119125                    next if /^\s*#/ || /^\s*$/; 
     
    124130                close MESSAGES; 
    125131            } else { 
    126                 carp "Could not read messages file $options{messages}: $!"; 
     132                carp "[Text::FormBuilder] Could not read messages file $options{messages}: $!"; 
    127133            } 
    128134        } 
    129     } 
     135    } else { 
     136        $options{messages} = { %DEFAULT_MESSAGES }; 
     137    } 
     138     
     139    my $charset = $options{charset}; 
    130140     
    131141    # save the build options so they can be used from write_module 
     
    133143     
    134144    # remove our custom options before we hand off to CGI::FormBuilder 
    135     delete $options{$_} foreach qw(form_only css extra_css); 
     145    delete $options{$_} foreach qw(form_only css extra_css charset); 
    136146     
    137147    # expand groups 
     
    230240            engine => { 
    231241                TYPE       => 'STRING', 
    232                 SOURCE     => $form_only ? $self->_form_template : $self->_template($css), 
     242                SOURCE     => $form_only ? $self->_form_template : $self->_template($css, $charset), 
    233243                DELIMITERS => [ qw(<% %>) ], 
    234244            }, 
     
    295305            engine => { 
    296306                TYPE       => 'STRING', 
    297                 SOURCE     => $self->{build_options}{form_only} ? $self->_form_template : $self->_template($css), 
     307                SOURCE     => $self->{build_options}{form_only} ?  
     308                                $self->_form_template :  
     309                                $self->_template($css, $self->{build_options}{charset}), 
    298310                DELIMITERS => [ qw(<% %>) ], 
    299311            }, 
     
    367379sub _form_template { 
    368380    my $self = shift; 
    369     #warn keys %{ $self->{build_options}{messages} }; 
    370     my $msg_required = $self->{build_options}{messages}{text_formbuilder_required}; 
    371     return q[<% $description ? qq[<p id="description">$description</p>] : '' %> 
    372 <% (grep { $_->{required} } @fields) ? qq[<p id="instructions">(Required fields are marked in <strong>bold</strong>.)</p>] : '' %> 
     381    my $msg_required = $self->{build_options}{messages}{text_required}; 
     382    my $msg_invalid = $self->{build_options}{messages}{text_invalid}; 
     383    return q{<% $description ? qq[<p id="description">$description</p>] : '' %> 
     384<% (grep { $_->{required} } @fields) ? qq[<p id="instructions">} . $msg_required . q{</p>] : '' %> 
    373385<% $start %> 
    374386<% 
    375387    # drop in the hidden fields here 
    376388    $OUT = join("\n", map { $$_{field} } grep { $$_{type} eq 'hidden' } @fields); 
    377 %> 
    378  
     389%>} . 
     390q[ 
    379391<% 
    380392    SECTION: while (my $section = shift @sections) { 
     
    387399                #TODO: we only need the field names, not the full field spec in the lines strucutre 
    388400                local $_ = $field{$$line[1]{name}}; 
     401                 
    389402                # skip hidden fields in the table 
    390403                next TABLE_LINE if $$_{type} eq 'hidden'; 
     
    401414                # mark invalid fields 
    402415                if ($$_{invalid}) { 
    403                     $OUT .= qq[<td>$$_{field} $$_{comment} Missing or invalid value.</td>]; 
     416                    $OUT .= "<td>$$_{field} $$_{comment} ] . $msg_invalid . q[</td>"; 
    404417                } else { 
    405418                    $OUT .= qq[<td>$$_{field} $$_{comment}</td>]; 
     
    434447} 
    435448 
     449# usage: $self->_pre_template($css, $charset) 
    436450sub _pre_template { 
    437451    my $self = shift; 
    438452    my $css = shift || $DEFAULT_CSS; 
     453    my $charset = shift || $DEFAULT_CHARSET; 
     454    my $msg_author = 'sprintf("' . quotemeta($self->{build_options}{messages}{text_author}) . '", $author)'; 
    439455    return  
    440456q[<html> 
    441457<head> 
     458  <meta http-equiv="Content-Type" content="text/html; charset=] . $charset . q[" /> 
    442459  <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> 
    443460  <style type="text/css"> 
     
    450467 
    451468<h1><% $title %></h1> 
    452 <% $author ? qq[<p id="author">Created by $author</p>] : '' %> 
    453 ]; 
     469<% $author ? qq[<p id="author">] . ] . $msg_author . q{ . q[</p>] : '' %> 
     470}; 
    454471} 
    455472 
    456473sub _post_template { 
    457 q[<hr /> 
     474    my $self = shift; 
     475    my $msg_madewith = 'sprintf("' . quotemeta($self->{build_options}{messages}{text_madewith}) . 
     476        '", q[<a href="http://formbuilder.org/">CGI::FormBuilder</a>], CGI::FormBuilder->VERSION)'; 
     477     
     478    return qq[<hr /> 
    458479<div id="footer"> 
    459   <p id="creator">Made with <a href="http://formbuilder.org/">CGI::FormBuilder</a> version <% CGI::FormBuilder->VERSION %>.</p> 
     480  <p id="creator"><% $msg_madewith %></p> 
    460481</div> 
    461482</body> 
     
    466487sub _template { 
    467488    my $self = shift; 
    468     my $css = shift || $DEFAULT_CSS; 
    469     return $self->_pre_template($css) . $self->_form_template . $self->_post_template; 
     489    return $self->_pre_template(@_) . $self->_form_template . $self->_post_template; 
    470490} 
    471491 
     
    554574used will be C<css> concatenated with C<extra_css>. 
    555575 
     576=item C<messages> 
     577 
     578This works the same way as the C<messages> parameter to  
     579C<< CGI::FormBuilder->new >>; you can provide either a hashref of messages 
     580or a filename. 
     581 
     582The default messages used by Text::FormBuilder are: 
     583 
     584    text_author       Created by %s 
     585    text_madewith     Made with %s version %s 
     586    text_required     (Required fields are marked in <strong>bold</strong>.) 
     587    text_invalid      Missing or invalid value. 
     588 
     589Any messages you set here get passed on to CGI::FormBuilder, which means 
     590that you should be able to put all of your customization messages in one 
     591big file. 
     592 
     593=item C<charset> 
     594 
     595Sets the character encoding for the generated page. The default is ISO-8859-1. 
     596 
    556597=back 
    557598 
     
    585626Calls C<render> on the FormBuilder form, and either writes the resulting 
    586627HTML to a file, or to STDOUT if no filename is given. 
    587  
    588 CSS Hint: to get multiple sections to all line up their fields, set a 
    589 standard width for th.label 
    590628 
    591629=head2 write_module 
     
    726764    field_3|'\'Official\' title' 
    727765 
    728 Now, back to the basics. Form fields are each described on a single line. 
     766Now, back to the beginning. Form fields are each described on a single line. 
    729767The simplest field is just a name (which cannot contain any whitespace): 
    730768 
     
    732770 
    733771This yields a form with one text input field of the default size named `color'. 
    734 The label for this field as generated by CGI::FormBuilder would be ``Color''. 
    735 To add a longer or more descriptive label, use: 
     772The generated label for this field would be ``Color''. To add a longer or more\ 
     773descriptive label, use: 
    736774 
    737775    color|Favorite color 
    738776 
    739 The descriptive label can be a multiword string, as described above. 
     777The descriptive label can be a multiword string, as described above. So if you 
     778want punctuation in the label, you should single quote it: 
     779 
     780    color|'Fav. color' 
    740781 
    741782To use a different input type: 
     
    853894Allow for custom wrappers around the C<form_template> 
    854895 
    855 Use the custom message file format for messages in the built in template (i18n/l10n) 
    856  
    857896Maybe use HTML::Template instead of Text::Template for the built in template 
    858897(since CGI::FormBuilder users may be more likely to already have HTML::Template) 
    859898 
    860899Better examples in the docs (maybe a standalone or two as well) 
    861  
    862 Document the defaults that are passed to CGI::FormBuilder 
    863900 
    864901C<!include> directive to include external formspec files 
     
    882919=head1 AUTHOR 
    883920 
    884 Peter Eichman <peichman@cpan.org> 
     921Peter Eichman C<< <peichman@cpan.org> >> 
    885922 
    886923=head1 COPYRIGHT AND LICENSE 
Note: See TracChangeset for help on using the changeset viewer.