[Grok-dev] Re: Grok Widgets / Fields (Was Re: Re: [grok-br] Grok
1.0 and beyond)
luciano at ramalho.org
Fri Jan 11 02:15:36 EST 2008
On Jan 10, 2008 1:37 PM, Sebastian Ware <sebastian at urbantalk.se> wrote:
> I use formlib but never got into using a form template. Instead I went
> for the simple solution, where I iterate over the widgets and use
> widget() to generate each individual widget. Something like this:
> outp = '<form class="edit-form" enctype="multipart/form-data"
> method="post" action="" accept-charset="UTF-8">\r'
> outp += '<table>'
> for widget in self.widgets:
> outp += '<tr class="row">\r'
> outp += '<td class="label"><label for="' + widget.name +
> '" title="' + widget.hint + '">'
> outp += widget.label + '</label></td>\r'
> outp += '<td class="field">' + widget() + '</td>\r'
> if widget.error() is not None:
> outp += '<td class="error">' + widget.error() + '</td>
> else: outp += '<td></td>\r'
> outp += '</tr>\r'
> outp += '</table>'
> outp += '</form>'
> Mvh Sebastian
Sebastian, the code in default_edit_form.pt also iterates over the
widgets to render them, it just does so using tal:repeat, not Python
BTW, you may be interested to know that your use of string
concatenation to build a page is highly inefficient. Python strings
are immutable, which means that the += operator is always copying the
entire string from one place in memory to another, over and over
again. If you must build long strings like this, a good practice is to
use a list, and then instead of the += do list.append() for each new
string segment. Then when everything is appended, you can do a
''.join(list) to transform the list of strings into one big string.
This is much faster.
Of course, the best practice for generating HTML is not to use Python
at all, but a template language such as ZPT or Genshi.
More information about the Grok-dev