Changes between Version 12 and Version 13 of GenshiRecipes/FormFilling
- Timestamp:
- Jul 4, 2007, 5:09:39 PM (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
GenshiRecipes/FormFilling
v12 v13 1 1 = [GenshiRecipes Genshi Recipes]: Automatically Populating an HTML Form = 2 2 3 == Using match templates == 3 The goal is to automatically add a `value` attribute to any `<input>` element with the appropriate data, and similar for other kinds of form controls. 4 4 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. 5 Assume the following template defining a simple form: 6 6 7 7 {{{ 8 #! xml8 #!genshi 9 9 <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 28 10 <form action="" method="POST"> 29 11 <!--! A simple text field --> … … 59 41 }}} 60 42 61 Assuming the context data contained a variable “form” that was set to: 43 == Using a Stream Flter == 44 45 The best option is to use the Genshi `HTMLFormFiller` stream filter that fills in form values. 62 46 63 47 {{{ 64 48 #!python 65 form = {'name': 'Look, Ma', 66 'enable': '', 67 'option': 'B', 68 'description': 'This is the description someone put in here', 69 'dropdown': '2'} 49 tmpl = loader.load('myform.html') 50 fill = 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 }) 57 print tmpl.generate().filter(fill).render('html') 70 58 }}} 71 59 72 The n theoutput would look as follows:60 The output would look as follows: 73 61 74 62 {{{ 75 #! xml63 #!text/html 76 64 <html> 77 65 <form action="" method="POST"> … … 111 99 }}} 112 100 113 == Using a template filter ==114 101 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 104 The 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 128 Assuming the context data contained a variable “form” that was set to: 116 129 117 130 {{{ 118 131 #!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') 132 form = {'name': 'Look, Ma', 133 'enable': '', 134 'option': 'B', 135 'description': 'This is the description someone put in here', 136 'dropdown': '2'} 128 137 }}} 129 138 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. 139 That would result in the same (or similar) output as above. 133 140 134 141 ----