Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) 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:
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 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.

jeffa

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)

In reply to (jeffa) Re: HTML::Template Form Usage by jeffa
in thread HTML::Template Form Usage by bladx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (9)
    As of 2021-04-14 12:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?