Edgewall Software

Changes between Version 12 and Version 13 of GenshiRecipes/FormFilling


Ignore:
Timestamp:
Jul 4, 2007, 5:09:39 PM (17 years ago)
Author:
cmlenz
Comment:

Cleanup

Legend:

Unmodified
Added
Removed
Modified
  • GenshiRecipes/FormFilling

    v12 v13  
    11= [GenshiRecipes Genshi Recipes]: Automatically Populating an HTML Form =
    22
    3 == Using match templates ==
     3The goal is to automatically add a `value` attribute to any `<input>` element with the appropriate data, and similar for other kinds of form controls.
    44
    5 The following template provides a match template that automatically adds a `value` attribute to any `<input>` element that has matching data in the context.
     5Assume the following template defining a simple form:
    66
    77{{{
    8 #!xml
     8#!genshi
    99<html xmlns:py="http://genshi.edgewall.org/">
    10 
    11   <input py:match="form//input[@type='text']" py:attrs="select('@*')"
    12          value="${form.get(str(select('@name')))}" />
    13   <input py:match="form//input[@type='checkbox']" py:attrs="select('@*')"
    14          checked="${form.get(str(select('@name')))}" />
    15   <input py:match="form//input[@type='radio']" py:attrs="select('@*')"
    16          py:with="value = form.get(str(select('@name')))"
    17          checked="${value and (value == str(select('@value'))) or None}" />
    18   <textarea py:match="form//textarea" py:attrs="select('@*')"
    19             py:content="form.get(str(select('@name')))"></textarea>
    20   <select py:match="form//select" py:attrs="select('@*')"
    21           py:with="select_name = str(select('@name'))"
    22           py:content="select('option|optgroup')" />
    23   <option py:match="form//select//option" py:attrs="select('@*')"
    24           py:with="sel_value = form.get(select_name)"
    25           selected="${sel_value and (str(select('@value')) == sel_value) or None}"
    26           py:content="select('text()')" />
    27 
    2810  <form action="" method="POST">
    2911    <!--! A simple text field -->
     
    5941}}}
    6042
    61 Assuming the context data contained a variable “form” that was set to:
     43== Using a Stream Flter ==
     44
     45The best option is to use the Genshi `HTMLFormFiller` stream filter that fills in form values.
    6246
    6347{{{
    6448#!python
    65 form = {'name': 'Look, Ma',
    66         'enable': '',
    67         'option': 'B',
    68         'description': 'This is the description someone put in here',
    69         'dropdown': '2'}
     49tmpl = loader.load('myform.html')
     50fill = HTMLFormFiller({
     51    'name': 'Look, Ma',
     52    'enable': 'on',
     53    'option': 'B',
     54    'description': 'This is the description someone put in here',
     55    'dropdown': '2'
     56})
     57print tmpl.generate().filter(fill).render('html')
    7058}}}
    7159
    72 Then the output would look as follows:
     60The output would look as follows:
    7361
    7462{{{
    75 #!xml
     63#!text/html
    7664<html>
    7765  <form action="" method="POST">
     
    11199}}}
    112100
    113 == Using a template filter ==
    114101
    115 The second, and maybe better, option, would be to use a custom template filter that filled in the form values. An example is [attachment:formfiller.py attached] to this page, and would be used like this:
     102== Using Match Templates ==
     103
     104The following template provides a match template that automatically adds a `value` attribute to any `<input>` element that has matching data in the context.
     105
     106{{{
     107#!genshi
     108  …
     109  <input py:match="form//input[@type='text']" py:attrs="select('@*')"
     110         value="${form.get(str(select('@name')))}" />
     111  <input py:match="form//input[@type='checkbox']" py:attrs="select('@*')"
     112         checked="${form.get(str(select('@name')))}" />
     113  <input py:match="form//input[@type='radio']" py:attrs="select('@*')"
     114         py:with="value = form.get(str(select('@name')))"
     115         checked="${value and (value == str(select('@value'))) or None}" />
     116  <textarea py:match="form//textarea" py:attrs="select('@*')"
     117            py:content="form.get(str(select('@name')))"></textarea>
     118  <select py:match="form//select" py:attrs="select('@*')"
     119          py:with="select_name = str(select('@name'))"
     120          py:content="select('option|optgroup')" />
     121  <option py:match="form//select//option" py:attrs="select('@*')"
     122          py:with="sel_value = form.get(select_name)"
     123          selected="${sel_value and (str(select('@value')) == sel_value) or None}"
     124          py:content="select('text()')" />
     125  …
     126}}}
     127
     128Assuming the context data contained a variable “form” that was set to:
    116129
    117130{{{
    118131#!python
    119 tmpl = loader.load('myform.html')
    120 fill = HTMLFormFiller({
    121     'name': 'Look, Ma',
    122     'enable': 'on',
    123     'option': 'B',
    124     'description': 'This is the description someone put in here',
    125     'dropdown': '2'
    126 })
    127 print tmpl.generate().filter(fill).render('xhtml')
     132form = {'name': 'Look, Ma',
     133        'enable': '',
     134        'option': 'B',
     135        'description': 'This is the description someone put in here',
     136        'dropdown': '2'}
    128137}}}
    129138
    130 That would result in the same output as above.
    131 
    132 '''Note''': the `HTMLFormFiller` class is available in the `genshi.filters` module starting from [milestone:0.4] release.
     139That would result in the same (or similar) output as above.
    133140
    134141----