Edgewall Software

Changes between Version 4 and Version 5 of GenshiRecipes/FormFilling


Ignore:
Timestamp:
Sep 24, 2006, 5:22:26 PM (18 years ago)
Author:
cmlenz
Comment:

Add match templates for checkboxes, radio groups, and text areas

Legend:

Unmodified
Added
Removed
Modified
  • GenshiRecipes/FormFilling

    v4 v5  
    88 
    99  <input py:match="form//input[@type='text']" py:attrs="select('@*')"
    10          value="${form[str(select('@name'))]}" />
    11  
     10         value="${form.get(str(select('@name')))}" />
     11  <input py:match="form//input[@type='checkbox']" py:attrs="select('@*')"
     12         checked="${form.get(str(select('@name')))}" />
     13  <input py:match="form//input[@type='radio']" py:attrs="select('@*')"
     14         py:with="value = form.get(str(select('@name')))"
     15         selected="${value and (value == str(select('@value'))) or None}" />
     16  <textarea py:match="form//textarea" py:attrs="select('@*')"
     17            py:content="form.get(str(select('@name')))"></textarea>
     18
    1219  <form action="" method="POST">
     20    <!--! A simple text field -->
    1321    <p>
    14       <label>Something: <input type="text" name="something"/></label>
     22      <label>Something: <input type="text" name="name"/></label>
    1523    </p>
     24    <!--! A check box -->
     25    <p>
     26      <label>Enabled: <input type="checkbox" name="enable" /></label>
     27    </p>
     28    <!--! A text area -->
     29    <p>
     30      <label for="description">Description</label>
     31      <textarea id="description" name="description">Bla bla bla</textarea>
     32    </p>
     33    <!--! A radio group -->
     34    <fieldset>
     35      <legend>Select one:</legend>
     36      <label py:for="option in ('A', 'B', 'C')">
     37        <input type="radio" name="option" value="$option" />
     38      </label>
     39    </fieldset>
    1640  </form>
    1741</html>
    1842}}}
    1943
    20 Assuming the context data contained a variable “form” that was `{'something': 'Look, Ma'}`, the output would look as follows:
     44Assuming the context data contained a variable “form” that was set to:
     45
     46{{{
     47#!python
     48form = {'name': 'Look, Ma',
     49        'enable': '',
     50        'option': 'B',
     51        'description': 'This is the description someone put in here'}
     52}}}
     53
     54Then the output would look as follows:
    2155
    2256{{{
     
    2559  <form action="" method="POST">
    2660    <p>
    27       <label>Something: <input value="Look, Ma" type="text" name="something" /></label>
     61      <label>Something: <input value="Look, Ma" type="text" name="name" /></label>
    2862    </p>
    29     <input value="Send" type="submit" />
     63    <p>
     64      <label>Enabled: <input type="checkbox" name="enable" /></label>
     65    </p>
     66    <p>
     67      <label for="description">Description</label>
     68      <textarea id="description" name="description">This is the description someone put in here</textarea>
     69    </p>
     70    <fieldset>
     71      <legend>Select one:</legend>
     72      <label>
     73        <input type="radio" name="option" value="A" />
     74      </label>
     75      <label>
     76        <input selected="selected" type="radio" name="option" value="B" />
     77      </label>
     78      <label>
     79        <input type="radio" name="option" value="C" />
     80      </label>
     81    </fieldset>
    3082  </form>
    3183</html>
    3284}}}
    3385
    34 This recipe would need to be extended to also support checkboxes, select fields, and so on.
     86This recipe would need to be extended to also support select fields.
    3587
    3688----