[Templates] creating dynamic hidden fields in templates

Brian Clarkson bclarkso@cisco.com
Wed, 06 Mar 2002 11:36:17 -0600


comments inline.

Craig Barratt wrote:

> > i have one line in my template to create hidden fields:
> >
> > [% cgi.hidden( Name => 'section' ).join %]
> >
> > which will work if there are multiple values for the param name
> > 'section'.  the above code fails to generate a hidden form element if
> > there is only one value associated with the name ( and somewhat
> > understandably ... it IS a one-element array, but i remember reading
> > somewhere in the message archives that one-element arrays getr
> > downgraded to scalars ... )
>
> That's right: one-element arrays get demoted to scalars.
>
> > so i tried this:
> >
> > [% hidden_name = [ cgi.param( Name => 'section' ).list ] %]
>
> No.  This is a single element list, that contains another list.
> hidden_name.size will always be 1.  You should write:
>
>     [% hidden_name = cgi.param( Name => 'section' ).list %]
>
> You can write the whole thing as:
>
>     [% cgi.hidden( Name => 'section' ).list.join %]
>
> This will work in both the scalar and array cases.

correct.  this does work, but i now end up with a blank value ( literally,
"" ) as one of the hidden fields.  not much of a problem, because i can
have my pre-processing logic handle that case explicitly.  but is there a
way to avoid it?

IIRC, CGI.pm's hidden method only outputs a hidden field if there is a
value for the corresponding name.  my memory may just not be accurate.

brian