Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: HTML::Template Form Usage

by jarich (Curate)
on Jan 03, 2002 at 09:44 UTC ( [id://135923]=note: print w/replies, xml ) Need Help??


in reply to HTML::Template Form Usage

...Retrieve data from MySQL via the DBI module -> display this data in HTML form -> make a form for someone to input a row in the MySQL DB ...

...But I don't know quite where to go with the code I have come up with which is displayed here:

I'm guessing that being told to use HTML::Template isn't exactly what you're looking for here, although it's very good advice.

If you're looking for advice on what to do to your code from here, such that you can do the DBI lookup, show the table entries and then offer an addition form then you might do the following:
tmpl file

<html> <title>test</title> <body> <TMPL_IF NAME=data> Your row was added in successfully.<p> </TMPL_IF> <h1>Data already in table</h1> <table> <TMPL_LOOP NAME=data> <tr> <td>Name:</td><td><TMPL_VAR NAME=box2></td> <td>Colour1:</td><td><TMPL_VAR NAME=box></td> </tr> </TMPL_LOOP> </table> <form> <table border=1 > <tr> <td> Color1: <TMPL_VAR NAME="BOX"> <br></td> <td> What is your name?: <TMPL_VAR NAME="BOX2"> <br> </td> <td> <input type=submit> </td> </table> </form> </body> </html>
I'm sure you can edit your cgi code appropriately to fill in that loop. Abstracting the html around the loop into your template file allows you to later change the back-grounds of your table rows or data or their fonts or many other things without having to wade through your code again. Of course if you don't like HTML source, you might not realise the benefit of this today.

Whether or not this duplicates loops depends on your code. If you do a query, then loop through all the results pushing the hashrefs you've fetched into an array and then give that to the template to loop through and display you've duplicated a loop. Perhaps you'd think it better to build your table inside your CGI code and then add that to a variable which you then display.

HTML::Template purists will hate that idea, and they should, because you'd be perverting a very powerful and beautiful tool, but if it's the best solution you have, then use it. Then again, I'd encourage you not to be afraid of having two similar loops here (using TMPL_IF to replace possible null fields with &nbsp might also help).

Jacinta

Replies are listed 'Best First'.
Re: Re: HTML::Template Form Usage
by rob_au (Abbot) on Jan 03, 2002 at 09:55 UTC
    HTML::Template purists will hate that idea, and they should, because you'd be perverting a very powerful and beautiful tool, but if it's the best solution you have, then use it. Then again, I'd encourage you not to be afraid of having two similar loops here (using TMPL_IF to replace possible null fields with   might also help).

    One little problem with the TMP_IF scenario is that, at least with HTML::Template version 2.4, a variable was classified as defined simply by it being passed to the template via the $template->param method. The value of the variable was not interpolated and this approach wouldn't work because although the variable is undefined, the act of simply passing it to the template would result in the variable being classified as 'defined' and able to be displayed.

    The solution I found was to make use of the HTML::Template extension module HTML::Template::Expr which allows conditional expressions to also be incorporated into the template file and as such gives you greater control over variable "definition".

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://135923]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-19 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found