| 1 | = Automatically Populating an HTML Form = |
| 2 | |
| 3 | The following template provides a match template that automatically adds a `value` attribute to any `<input>` element that has matching data in the context. |
| 4 | |
| 5 | {{{ |
| 6 | #!xml |
| 7 | <html xmlns:py="http://markup.edgewall.org/"> |
| 8 | |
| 9 | <input py:match="form//input" py:attrs="select('@*')" |
| 10 | value="${form[str(select('@name'))]}" /> |
| 11 | |
| 12 | <form action="" method="POST"> |
| 13 | <p> |
| 14 | <label>Something: <input type="text" name="something"/></label> |
| 15 | </p> |
| 16 | </form> |
| 17 | </html> |
| 18 | }}} |
| 19 | |
| 20 | Assuming the context data contained a variable “form” that was `{'something': 'Look, Ma'}`, the output would look as follows: |
| 21 | |
| 22 | {{{ |
| 23 | #!xml |
| 24 | <html> |
| 25 | <form action="" method="POST"> |
| 26 | <p> |
| 27 | <label>Something: <input value="Look, Ma" type="text" name="something" /></label> |
| 28 | </p> |
| 29 | <input value="Send" type="submit" /> |
| 30 | </form> |
| 31 | </html> |
| 32 | }}} |
| 33 | |
| 34 | This recipe would need to be extended to also support checkboxes, select fields, and so on. |
| 35 | |
| 36 | ---- |
| 37 | See also: MarkupRecipes, MarkupTemplates |