[Grok-dev] Re: Grok Widgets / Fields (Was Re: Re: [grok-br] Grok
1.0 and beyond)
sebastian at urbantalk.se
Fri Jan 11 04:34:02 EST 2008
11 jan 2008 kl. 08.15 skrev Luciano Ramalho:
> On Jan 10, 2008 1:37 PM, Sebastian Ware <sebastian at urbantalk.se>
>> I use formlib but never got into using a form template. Instead I
>> 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() + '</
>> 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
Maybe I should try to get into the formlib stuff again. The code seems
to be well documented, but sometimes I have difficulties translating
the doctest type documentation into working code.
> 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.
Thanks! I should do something about that... :)
> Of course, the best practice for generating HTML is not to use Python
> at all, but a template language such as ZPT or Genshi.
People keep saying this. Maybe I just need to get with the program. :)
More information about the Grok-dev