|Problems? Is your data what you think it is?|
(jeffa) Re: HTML::Template Form Usageby jeffa (Bishop)
|on Jan 03, 2002 at 06:08 UTC||Need Help??|
What rob_au said is mighty true bladx. So I will focus on something else: CGI forms.
What you are missing is the action attribute for the <form> tag. Also you are missing the fact that you don't have to have one template param for the text box and another for the value ... but let's back up a bit and slim this puppy down.The following is a super simple CGI script. Save this, run it, study it, know it:
It is called a reentrant script, because it does one of two different things depending upon what parameters it finds:
Now, how does this look when used with HMTL::Template? First off, go back to HTML::Template Tutorial and re-read 'Bag of Tricks' which describes associate. Here is the HTML::Template version:
Even though CGI.pm is used, it is only used to print the header, to find the name of the script at runtime, and to associate it's parameters with HTML::Template.
Not to generate HTML.
And yes, the template is included in the Perl script, which is NOT the same as including the HTML among your Perl code. This is a neat trick i like to use for my own scripts that only need one template.The HMTL::Template version is twice as large - the code itself is about the same, but it is more complex. Why would you want to solve a problem like this with HTML::Template then?
UPDATE: A better question:
What are the benefits of using HTML::Template instead of CGI for this example?
- To have complete control over the HTML is a good reason. I mean _complete_ control. If you don't need that kind of control over the HTML, then CGI.pm probably is the better solution.
- You don't have mingle any HTML in your code. In the CGI.pm example, even though i managed to use methods to generate all tags, the layout is still dependent upon the code (via which methods are called when). This can hurt when you decide to change the layout. With a properly templated solution, the code has nothing to do with the layout. The code just passes the data.
- Does this one count? I didn't have to worry about querying for form parameters in the HMTL::Template version, CGI.pm did it for me! You won't always be able to get away with this, however.
What are the benefits of CGI.pm over HTML::Template?
- Saving time is the only one i can think of, but that is always the most important benefit RIGHT NOW. It took me very little time (with the exception of RTFM'ing for 2 small CGI.pm tricks) to come up with the CGI.pm example, and quite a bit more time for the HTML::Template version. Adding loops really increases your production time, but it does hone your anonymous data structure skills as a nice side effect.
Last note, keep in mind that Q / A and HTML::Template techniques... talks about concepts that go waaaay beyond those of my tutorial. Also, instead of using CGI.pm to handle CGI, mod_perl is used. That post is gold, by the way. ++maverick!
Final last note, don't forget that HTML::Template is only one of many templating CPAN modules.
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR F--F--F--F--F--F--F--F-- (the triplet paradiddle)