Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

(jeffa) Re: HTML::Template Form Usage

by jeffa (Bishop)
on Jan 03, 2002 at 06:08 UTC ( #135889=note: print w/replies, xml ) Need Help??

in reply to HTML::Template Form Usage

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:
use strict; use CGI qw(:standard); my $box; print header, start_html('test'), start_form, textfield(-name=>'box',-override=>1), submit('go'), end_form; if ($box = param('box')) { print 'You submitted' . div({style=>'display: inline; color: red'}, +$box); }
It is called a reentrant script, because it does one of two different things depending upon what parameters it finds:
  1. if "box" was not found, then this is the first time 'through the script' - ask the question
  2. if "box" is found, then this is the second time - parse the answer
Instead of just 'giving the answer', i also 'ask the question' again, to keep the user from having to go back themselves. And actually, there is no 'second time' - only the first time and 'the rest'. What if the answer is wrong? What if the user didn't give any answer? These questions are the 'business rules', and are not even addressed here. This script only shows the user what they submitted, or nothing if they press 'go' without giving an answer. You should be able to figure out how to add the code that pulls and stores values from your database to this framework now that you see how to get params from the user.

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:

use strict; use CGI; use HTML::Template; my $cgi = CGI->new(); my $data = do { local $/; <DATA> }; my $template = new HTML::Template( scalarref => \$data, associate => $cgi, ); $template->param( url => $cgi->script_name(), ); print $cgi->header(), $template->output(); __DATA__ <html> <title>test</title> <body> <form method="POST" action="<tmpl_var name=url>"> <input type="text" name="box"> <input type="submit" name="go" value="go"> </form> <tmpl_if name="box"> You submitted <div style="display: inline; color: red"> <tmpl_var name="box"> </div> </tmpl_if> </body> </html>
Even though 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 probably is the better solution.

- You don't have mingle any HTML in your code. In the 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, did it for me! You won't always be able to get away with this, however.

What are the benefits of 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 tricks) to come up with the 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 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.


(the triplet paradiddle)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://135889]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-10-19 20:16 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (257 votes). Check out past polls.