[Petal] option selected

Kurt Stephens kstep at pepsdesign.com
Sun Dec 19 17:33:44 GMT 2004


Hi All,

If you are using CGI.pm or a comparable module for your form
processing I might suggest using the CGI object itself to render
the HTML for your form controls and the tal:replace attribute to
substitute the pre-rendered HTML in the template.  One advantage
of this is that it maintains the 'sticky' behavior provided by
CGI.pm without having to copy data from the query object to the
Petal hash.  To keep the MVC purists happy, any fancy formatting
of the form widgets should be performed via CSS using either
class or id attributes.

The following example shows a script that populates a select box
using tal:replace:

In the script:

my $query = new CGI();
my $template = Petal->new('cgiform.html');
my $hashref = {};

$hashref->{widgets}->{popupMenu} = $query->popup_menu(
    -name       => 'popupMenu',
    -values     => ['1st', '2nd', '3rd'],
    -default    => '2nd',
    -labels     => {
        '1st'       => 'First Place',
        '2nd'       => 'Second Place',
        '3rd'       => 'Third Place'
        },
    -class      => 'myPopupClass',
    -id         => 'popupMenu',
    );

------

In the template:

<select name="popupMenu"
        size="1"
        id="popupMenu"
        class="myPopupMenuClass"
        tal:replace="structure widgets/popupMenu">
  <option value="option1"
          selected="selected">Option 1</option>
  <option value="option2">Option 2</option>
  <option value="option3">Option 3</option>
</select>

For what it's worth...

Kurt Stephens



More information about the Petal mailing list