[Petal] How do I use selected/checked ?
Raymond Barkhouse
raymond at print-quotes-software.com
Thu Sep 1 16:31:30 BST 2005
On Thu, 2005-01-09 at 10:48 +0200, Olaf Buwen wrote:
> This template:
>
> <select>
> <option
> petal:repeat="entry options"
> petal:attributes="value entry/lang; selected entry/selected"
> petal:content="entry/title"/>
> </select>
>
> with this code:
> ...
> print $template->process(
> options => [
> {lang => "foo", title => "Foobar"},
> {lang => "bar", title => "Barbar", selected => 'selected'},
> {lang => "baz", title => "Bazbar"},
> ]
> );
>
> gives this output:
> <select>
> <option value="foo" >Foobar</option>
> <option value="bar" selected="selected">Barbar</option>
> <option value="baz" >Bazbar</option>
> </select>
>
That's the same method I use for very dynamic forms. For just filling in
form variables from a database/Apache::Request->param/CGI->param though
you might want to consider using HTML::FillInForm.
Given a hash (
foo => 'quibble',
bar => 'qax',
baz => '3',
);
Some form elements:
<input name="foo" type="text" value="" />
<input name="bar" type="radio" value="qux" />
<input name="bar" type="radio" value="qax" />
<input name="bar" type="radio" value="quz" />
<select name="baz">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
And a slight change to your perl:
$r->print(HTML::FillInForm->new->fill(
fdata => \%hash,
scalarref => \$t->process($other_stuff),
));
Will give you:
<input name="foo" type="text" value="quibble" />
<input name="bar" type="radio" value="qux" />
<input name="bar" type="radio" value="qax" checked="checked" />
<input name="bar" type="radio" value="quz" />
<select name="baz">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3" selected="selected">Three</option>
<option value="4">Four</option>
</select>
Much cleaner at the expense of some processing. I'm currently working on
doing the substitution at the same time Petal is processing the XML
stream as there's no need to process it twice (as it currently
happening). If anyone else is working in that direction I'd be
interested in hearing from you.
More information about the Petal
mailing list