Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
We don't bite newbies here... much
 
PerlMonks  

Template Stuff

by bar10der (Beadle)
on Jun 11, 2004 at 10:22 UTC ( #363348=perlquestion: print w/ replies, xml ) Need Help??
bar10der has asked for the wisdom of the Perl Monks concerning the following question:

I have an html form which has 60+ fields. To give a brief overview basically I am asking users to provide monthly status of accounts that they manage. I thought it will be good idea to populate form with data from previous month as value for many fields may not change every month. Data is stored in oracle table and I am able to extract data from oracle table. No problem here.br I have my html form and this is how I am loading form after getting data from oracle table (stored in @data)-
my $template = $self->load_tmpl('acc.html'); $template->param($errs) if ref $errs; ##Validate input $template->param( MONTH => $monthName, YEAR => $whichYear, ACC => $acName, LEFT => $lstring, f0 => $data[0], f1 => $data[1], ... ... f..n => $data[..n], ); return $template->output;
Above is working fine but in case a new field is added or discarded, I have to change my script and I want to avoid this. HTML form field names are same as in oracle table's column name and I can extract fieldname and corresponding value from the tables and all that I want to know - is there a better way for assigning values to the form fields? Any help will be greatly appreciated.

Comment on Template Stuff
Download Code
Re: Template Stuff
by matija (Priest) on Jun 11, 2004 at 10:49 UTC
    The param method returns the names of the fields in the template (if called without parameters):
    $oracle_row=$sth->fetchrow_hashref; foreach ($template->param) { param($_=>$oracle_row{$_}); } return $template->output;
Re: Template Stuff
by BUU (Prior) on Jun 11, 2004 at 10:51 UTC
    If I undestand you (and with some handwaving), this should work:
    #furiously waving hand! my %fields = get_fields(); # field name => old value my @fields; for(keys %fields ) { push @fields, { fname => $_, fvalue => $fields{$_} }; } my $tmpl = HTML::Template->new(); $tmpl->params( old_fields => \@fields ); print $tmpl->output;
    Then in your template code:
    <tmpl_loop name='old_fields'> <input type='text' name='<tmpl_var fname>' value='<tmpl_var fvalue>' </tmpl_loop>
    You probably want to use escape=html in the tmpl tags, but i've omitted them for the sake of brevity.
Re: Template Stuff
by neniro (Priest) on Jun 11, 2004 at 10:53 UTC
    Always use an AoH for this. You can get your result as hash from DBI and push it into an array. Use a loop in your templates to fill your table. Of course you still have to change your templates but you don't have to maintain your code if you make changes to your database.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://363348]
Approved by Paulster2
Front-paged by paulbort
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-04-17 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls