http://www.perlmonks.org?node_id=121510

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

If you've worked with CGI.pm for any length of time, you know that forms are automatically sticky when using the HTML generating functions. However, when using alternatives such as HTML::Template or Template Toolkit (which is what I am using), then you have to manually add the 'sticky' action to the templates. This can be a lot of work. For example, I might have some HTML like this:

<input type="text" name="username" value="" maxlength="20" />

For the above very simple snippet, I would change it to:

<input type="text" name="username" value="[% username %]" maxlengt +h="20" />

Naturally, it becomes considerably more complicated when dealing with <select> tags and things like that.

Of course, then I need to go through and write the code that actually grabs and untaints the data and, if incomplete, returns the user to the form with this data already filled in. After writing this stuff for the umpteenth time, I've decided to write a code generator which will create the template and the shell of the CGI script for me. I'm basing this on this code generator that I wrote a while ago. However, this has been a fair amount of work and I'm curious to know if this has already been done. I haven't found any code out there that already does this, but this is so common that I can't imagine that no one has done this before.

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Sticky HTML Templates
by Masem (Monsignor) on Oct 25, 2001 at 22:16 UTC
    Unless I misread the docs and added unnecessary default statements in my first foray with TT2, if you use a [% USE CGI %] and the appropriate CGI calls with the TT2 document, it's as sticky as perl's CGI.pm calls.

    Update Expanding as requested by boo below:

    If I write this template and call it from my perl code as given:

    [% USE CGI %] [% CGI.header %] [% CGI.h1( "Hello World!" ) %] [% CGI.start_form %] [% CGI.textbox( { name=>"text" } ) %] [% CGI.submit %] [% CGI.end_form %]
    Then in effect, ignoring template substitutions that may happen, it is the same as this perl code:
    #!/usr/bin/perl use CGI; my $q = new CGI; print $q->header(), $q->h1( "Hello World!" ), $q->start_form(), $q->textbox( -name=>"text" ), $q->submit, $q->end_form;
    Specifically, the CGI object as created by TT2 acts just like the CGI object in perl, and thus knows how to capture the passed parameters and knows how to fill in default values. There's minimal docs on this here, but from my experience working with it, that appears to be the case. Please note the code above is OTTOMH, only to demonstrat the point.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      Please expand on this.

        I'm playing with this now. Here's what I have:

        <textarea cols="45" rows="3" name="event"></textarea>

        This becomes:

        [% USE CGI %] <textarea cols="45" rows="3" name="event">[% CGI.param('event') %] +</textarea>

        It performs as advertised, but doesn't escape any of the HTML characters, so typing </textarea> in the textarea will break it. It appears that the HTML plugin can fix this, but I don't have that installed (yet).

        I'm going to have to give this some thought. It's a quick and easy way to make some things sticky, but I don't see how it would work with a select group, for example. I'll second your request to have masem expand on this :)

        Update: Hmm... I see what masem is doing, but that kind of defeats part of what I need: a simple, easily maintainable template that our designers can go into a work on. Interesting, though.

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Sticky HTML Templates
by perrin (Chancellor) on Oct 26, 2001 at 00:33 UTC
    I would have suggested what Masem did with the CGI plugin, but you could also post-process your HTML with HTML::FillInForm.

      I read through HTML::FillInForm and have to admit that it would be a nifty solution. I can see pros and cons, however.

      Pros: As the HTML would be left relatively pristine, it would be much easier for our designers to work with it. Further, as the HTML changes, there would less synchronization. This is a huge win. This also appears to be a relatively easy solution to implement.

      Cons: We're running in an ISAPI environment here. While that's faster than straight CGI, it does not compare to the performance of mod_perl. Until such time that we can convince the Powers That Be that mod_perl (and Apache, and Linux and ...) is the way to go, we have to be careful about the performance of our applications. Already, using Template Toolkit is a serious issue, in terms of performance. I'm concerned that HTML::FillInForm would be the death knell. Any experience with how this might affect real-world performance? It seems to me that inserting the template markup tags directly into the HTML document would be the least performance-intensive alternative.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        I don't know how well it performs, but if you're having performance problems and need to use IIS, I would suggest PerlEx or Velocigen. (Is there a FastCGI for IIS?)