in reply to Template::Toolkit: better way to generate variable names?

If you create 5 copies of a form element with the same name CGI.pm will return the contents of all occurances of the
that fieldname as an array of the values in the order they were specified.

Update
MidLifeXis has a valid point concerning the order in which the values will be returned. So if the numbers are necessary to specify some type of ordering or precedence go with this revelation

This will simplify your code immensely when you need to drop back to 3 floor plans or increase to 7 floor plans.

In your template:
[% FOREACH s = [ 1 .. 5 ] %] <tr> <td> Floor Plan Name </td> <td colspan='3'> [% mycgi.textfield( '-name' => floorplan_name, '-size' => '50', '-default' => THIS_PAGE.$IDX.floorplan_na +me ) %] </td> </tr> [% END %]
In your form processing cgi:
my $q = CGI->new; my @floorplans = $CGI->param('floorplan_name'); # How many plans arrived? my $num_plans = @floorplans; # Looping over my plans foreach my $plan ( @floorplans ) { &do_the_plan( $plan ) }
--
Clayton aka "Tex"
  • Comment on You don't even need to do that (was Re: Template::Toolkit: better way to generate variable names?)
  • Select or Download Code

Replies are listed 'Best First'.
Re: You don't even need to do that (was Re: Template::Toolkit: better way to generate variable names?)
by MidLifeXis (Monsignor) on Aug 06, 2003 at 16:45 UTC

    However (from my CGI.pm),

    NOTE: As of version 1.5, the array of parameter names returned will be in the same order as they were submitted by the browser. Usually this order is the same as the order in which the parameters are defined in the form (however, this isn't part of the spec, and so isn't guaranteed).

    I don't think you can rely 100% on this behaviour, especially if you are going to submit foo_1, bar_1, foo_2, bar_2, etc.

      I don't think you can rely 100% on this behaviour, especially if you are going to submit foo_1, bar_1, foo_2, bar_2, etc.
      that's pretty much my point in appending the indicator numbers. i don't want to depend too much on side-effects. the usually part of the perldoc is what bothers me. i need always, not usually

      plus, there are about 7 other variables which go along with the set. so ... i need to remain defensive about data integrity.