Re: option selected

"Kurt Stephens" <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.petal
Message-ID <002e01c4e5f0$e3d33d00$0301a8c0@ishmael>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.