|Version 2 (modified by michele, 10 years ago) (diff)|
This page aims to collect opinions regarding TurboGears Widgets.
If you don't know TG widgets take a look at this message from Kevin.
Feel free to add your own opinions and comments to this page.
Actually to use a widget you need to do something like this:
- Create an instance of the widget from your controller
... age_field = widgets.TextField("age", default=0, validator=validators.Int()) ...
- Insert your widget instance inside your template
... <div py:replace="age_field.insert(age)"/> ...
IMHO for the second point we can use Kid py:match attribute to do something like this:
... <widget name="age_field" value="age" /> ...
it shouldn't be to hard if we add to the sitetemplate a definition like this (I have not tested it):
... <span py:match="item.tag == 'widget'" py:replace="getattribute(self, item.get('name'), None).insert(item.get('value'))"> </span> ...
Customizing Widgets Look&Feel
Look&Feel customization is a quite important aspect for widgets. Actually you have two options:
- Providing a custom CSS stylesheet
- Providing a custom Kid template
Customization should be an easy task and should happen inside our templates (not the controller).
Widgets should use CSS class and selector to make it straigthforward.
During the 0.9 IRC Chat all have agreed that widgets should use namespaced class names.
Ability to set common attributes used for widgets
There is a ticket (#106) regarding this. This is an import requirement that will make customization even more easy. Using the syntax I proposed above customization shouldn't be that hard and inside your template you should be able to do the following:
... <widget name="age_field" value="age" size="30" id="id_age_field"/> ...
But, how can we make customization as easy also for complex widgets that embeds others widgets? For example the Form widget? Any opinion?
What about a class of widget that knows about their validation state?
Imagine a Form widget that let's you do something like this (only a pseudo code to show the general concept):
@turbogears.expose(template="...") def index(self): myform = TableForm(widgets=[ widgets.TextField("name"), widgets.TextField("address"), widgets.TextField("age", default=0, validator=validators.Int())]) return dict(form=myform) @turbogears.expose(input_form=myform) def save(self, input_form): if not input_form.valid: return self.index() else: [save in the db and takes parameters directly from the input_form (ex. input_form.name, input_form.address) not cherrypy.request.input_values]
This way you shouldn't need a special has_error variable and the need to check against cherrypy.request.form_errors and to take values from cherrypy.request.input_values.
To me it seems just more natural.