[Templates] creating dynamic hidden fields in templates

Craig Barratt craig@arraycomm.com
Tue, 05 Mar 2002 18:19:47 -0800


> 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.

Craig