nodata has asked for the wisdom of the Perl Monks concerning the following question:

I've got an online form!

To avoid code and HTML mixage, I'm trying desperately to keep everything separate: the HTML is read from a file on disk, mixed around, then displayed to the user after some Perl magic.

I plan to use HTML::Defaultify to choose sensible defaults in the form.
I plan to use HTML::FillInForm to keep the form sticky.

Everything looks good so far - but how can I initially populate the form with values (based on the person using the form)?

Ideally I'd like to do something like:

my $html = &get_html(); my $pop = new Form::Populator; if (! $cgi->param('validate') ) { my $new_html = $pop->populate( computers => qw/ abra cada bra /, staff => qw/ alice bob chris / ) } # $html now contains a nice bit of HTML that has had # values added to the list boxes with name "computers" # and "staff".

I'd really rather avoid a full blown template solution like the Template Toolkit.

Any ideas? THANKS!

Replies are listed 'Best First'.
Re: Populating forms
by Corion (Patriarch) on Mar 29, 2004 at 09:16 UTC

    While you may want to avoid a "full blown" templating solution like Template Toolkit, there are various "weaker" templating flavours:

    • Interpolation.pm - a nice hack which can be abused to be a close-to-the-metal templating sytem. It has no niceties like automagic HTML/URL encoding.
    • HTML::Template - a very simplicistic templating system, geared towards production of HTML. Can escape HTML entities or values for use as URL strings. Comes as one .pm file, which makes installation trivial.
    • Petal - a nice (x)HTML templating module that, in addition allows method calls on objects through the TAL syntax. It requires its input to be proper XHTML, and it is a bit slow even though it implements caching.
    • Template Toolkit - a full templating system with its own templating language.

    I've listed the templating systems in increasing order of features and I believe you should use HTML::Template, as it is very simplicistic but has enough features to be useful.

      Thanks for that. I've taken another look at HTML::Template, and it seems to be the best option.

      I think I was hoping more that there would be nice OO interface to add items to forms, but I now realise this would get ugly for anything like radio boxes and check boxes. (List boxes would be easy, just look for <SELECT NAME="something"></SELECT> then populate).

      HTML::Template seems like the best bet. Thanks!

      HTML::FillInForm
      http://search.cpan.org/dist/HTML::FillInForm
      Data::FormValidator
      http://search.cpan.org/dist/Data::FormValidator

      will be your best bets

      --
      Clayton
Re: Populating forms
by Happy-the-monk (Canon) on Mar 29, 2004 at 09:05 UTC

    The CGI module you're already using has a   -value/-values   switch to many of its functions/methods. Just use that.

      I can't see how I can get CGI.pm to append to an existing piece of HTML, so should I do something like

      my $snippet = $cgi->checkbox_group( -name=>'foo', -value=>['aaa', 'bbb', 'ccc'] );

      then replace [% SOMETHING %] with $snippet?

        I haven't learned much of your approach yet, but if that's the way you build your web pages/templates, it's a viable option.

        Cheers, Sören

        20040329 Edit by Corion: Reparented as per authors request