Changeset 34 in text-formbuilder for trunk/lib/Text
- Timestamp:
- 11/19/04 09:44:42 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Text/FormBuilder.pm
r33 r34 32 32 END 33 33 34 # default messages that can be localized 34 35 my %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.', 38 40 ); 41 42 my $DEFAULT_CHARSET = 'iso-8859-1'; 39 43 40 44 sub new { … … 98 102 # neat trick: extra_css => '@import(my_external_stylesheet.css);' 99 103 # 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 100 106 my $css; 101 107 $css = $options{css} || $DEFAULT_CSS; … … 115 121 # filename, just *warn* on missing, and use defaults 116 122 if (-f $options{messages} && -r _ && open(MESSAGES, "< $options{messages}")) { 117 $options{messages} = { };123 $options{messages} = { %DEFAULT_MESSAGES }; 118 124 while(<MESSAGES>) { 119 125 next if /^\s*#/ || /^\s*$/; … … 124 130 close MESSAGES; 125 131 } else { 126 carp " Could not read messages file $options{messages}: $!";132 carp "[Text::FormBuilder] Could not read messages file $options{messages}: $!"; 127 133 } 128 134 } 129 } 135 } else { 136 $options{messages} = { %DEFAULT_MESSAGES }; 137 } 138 139 my $charset = $options{charset}; 130 140 131 141 # save the build options so they can be used from write_module … … 133 143 134 144 # 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); 136 146 137 147 # expand groups … … 230 240 engine => { 231 241 TYPE => 'STRING', 232 SOURCE => $form_only ? $self->_form_template : $self->_template($css ),242 SOURCE => $form_only ? $self->_form_template : $self->_template($css, $charset), 233 243 DELIMITERS => [ qw(<% %>) ], 234 244 }, … … 295 305 engine => { 296 306 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}), 298 310 DELIMITERS => [ qw(<% %>) ], 299 311 }, … … 367 379 sub _form_template { 368 380 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>] : '' %> 373 385 <% $start %> 374 386 <% 375 387 # drop in the hidden fields here 376 388 $OUT = join("\n", map { $$_{field} } grep { $$_{type} eq 'hidden' } @fields); 377 %> 378 389 %>} . 390 q[ 379 391 <% 380 392 SECTION: while (my $section = shift @sections) { … … 387 399 #TODO: we only need the field names, not the full field spec in the lines strucutre 388 400 local $_ = $field{$$line[1]{name}}; 401 389 402 # skip hidden fields in the table 390 403 next TABLE_LINE if $$_{type} eq 'hidden'; … … 401 414 # mark invalid fields 402 415 if ($$_{invalid}) { 403 $OUT .= qq[<td>$$_{field} $$_{comment} Missing or invalid value.</td>];416 $OUT .= "<td>$$_{field} $$_{comment} ] . $msg_invalid . q[</td>"; 404 417 } else { 405 418 $OUT .= qq[<td>$$_{field} $$_{comment}</td>]; … … 434 447 } 435 448 449 # usage: $self->_pre_template($css, $charset) 436 450 sub _pre_template { 437 451 my $self = shift; 438 452 my $css = shift || $DEFAULT_CSS; 453 my $charset = shift || $DEFAULT_CHARSET; 454 my $msg_author = 'sprintf("' . quotemeta($self->{build_options}{messages}{text_author}) . '", $author)'; 439 455 return 440 456 q[<html> 441 457 <head> 458 <meta http-equiv="Content-Type" content="text/html; charset=] . $charset . q[" /> 442 459 <title><% $title %><% $author ? ' - ' . ucfirst $author : '' %></title> 443 460 <style type="text/css"> … … 450 467 451 468 <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 }; 454 471 } 455 472 456 473 sub _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 /> 458 479 <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> 460 481 </div> 461 482 </body> … … 466 487 sub _template { 467 488 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; 470 490 } 471 491 … … 554 574 used will be C<css> concatenated with C<extra_css>. 555 575 576 =item C<messages> 577 578 This works the same way as the C<messages> parameter to 579 C<< CGI::FormBuilder->new >>; you can provide either a hashref of messages 580 or a filename. 581 582 The 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 589 Any messages you set here get passed on to CGI::FormBuilder, which means 590 that you should be able to put all of your customization messages in one 591 big file. 592 593 =item C<charset> 594 595 Sets the character encoding for the generated page. The default is ISO-8859-1. 596 556 597 =back 557 598 … … 585 626 Calls C<render> on the FormBuilder form, and either writes the resulting 586 627 HTML 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 a589 standard width for th.label590 628 591 629 =head2 write_module … … 726 764 field_3|'\'Official\' title' 727 765 728 Now, back to the b asics. Form fields are each described on a single line.766 Now, back to the beginning. Form fields are each described on a single line. 729 767 The simplest field is just a name (which cannot contain any whitespace): 730 768 … … 732 770 733 771 This 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 moredescriptive label, use:772 The generated label for this field would be ``Color''. To add a longer or more\ 773 descriptive label, use: 736 774 737 775 color|Favorite color 738 776 739 The descriptive label can be a multiword string, as described above. 777 The descriptive label can be a multiword string, as described above. So if you 778 want punctuation in the label, you should single quote it: 779 780 color|'Fav. color' 740 781 741 782 To use a different input type: … … 853 894 Allow for custom wrappers around the C<form_template> 854 895 855 Use the custom message file format for messages in the built in template (i18n/l10n)856 857 896 Maybe use HTML::Template instead of Text::Template for the built in template 858 897 (since CGI::FormBuilder users may be more likely to already have HTML::Template) 859 898 860 899 Better examples in the docs (maybe a standalone or two as well) 861 862 Document the defaults that are passed to CGI::FormBuilder863 900 864 901 C<!include> directive to include external formspec files … … 882 919 =head1 AUTHOR 883 920 884 Peter Eichman <peichman@cpan.org>921 Peter Eichman C<< <peichman@cpan.org> >> 885 922 886 923 =head1 COPYRIGHT AND LICENSE
Note: See TracChangeset
for help on using the changeset viewer.