[Petal] Interpolating Form Data Tactic

Jonathan petal-list at 2xlp.com
Wed Oct 26 16:33:20 BST 2005


I've settled on a standard method that i've found super comfortable  
for interpolating form data with static TAL files.

Just wanted to share it with the list, as a few friends who tried  
this really liked this way of setting up data structure for  
interpolation and have adopted it  too.

I create a 'Defaults' data structure for the form data with _checked,  
_text and _selectxml/_selected sub-structures to  handle form info  
based on the type

the _selectxml data is not processed in tal, it easily could be -- i  
just had already written a bunch of routines that already did that  
stuff outside of tal.

anyways, the perl looks like this:

my %Defaults'     =
(
     '_checked'     =>
     {
         'fieldname' =>
         {
             'fieldvalue' => 'checked'
         }
     }
     ,
     '_text' =>
     {
         'fieldname' = 'fieldvalue',
     },
     '_selected' =>
     {
         'fieldname' = 'selected',
     },
     '_selectxml' =>
     {
         'fieldname' = 'structured xml',
     },
);

and the html looks like this:

Text
<input type="text" name="firstname" value="Defaults/_text/firstname" / 
 > First Name

Radio
<input type="radio" name="gender" value="male"  
tal:attributes="checked Defaults/_checked/gender/male" /> Male
<input type="radio" name="gender" value="female"  
tal:attributes="checked "Defaults/_checked/gender/female" /> Female

Checkbox
<input type="checkbox" name="optin" value="yes"  
tal:attributes="checked "Defaults/_checked/optin/yes" /> Yes

Multiple Checkboxes
<input type="checkbox" name="optin_id" value="1"  
tal:attributes="checked "Defaults/_checked/optin/1" /> Item 1
<input type="checkbox" name="optin_id" value="2"  
tal:attributes="checked "Defaults/_checked/optin/2" /> Item 2
<input type="checkbox" name="optin_id" value="3"  
tal:attributes="checked "Defaults/_checked/optin/3" /> Item 3

Select
<select name="state" tal:content="structure Defaults/_selectxml/state">
<option value="fake">will be replaced</option>
</select>

or

Select
<select name="state">
   <option value="ny" tal:attributes="selected Defaults/_selected/ 
state/ny">NY</option>
</select>


It works really well for me - and it's made my perl and html much  
more readable and accessible to others.



More information about the Petal mailing list