deform
deform copied to clipboard
Widget should own its renderer
When extending a widget (e.g., to use a template with a different rendering engine), we're usually stumbling upon the fact that the renderer used to render a template is an instance variable on the Field. Consider this code example:
class ColorChooserWidget(TextInputWidget):
template = "forms/color_chooser.jinja2"
def serialize(self, field, cstruct, **kw):
original_renderer = field.renderer
field.renderer = lambda template, **kwargs: render(self.template, kwargs)
rendered = super(ColorChooserWidget, self).serialize(field, cstruct, **kw)
field.renderer = original_renderer
return rendered
This works, but I do not see why the renderer would have to be set on the field. It would be much more sensible if the widgets would go through a self.render() which is easy to override.
When doing this, it should be considered that the renderer may be different for the main template and the item_template in mapping/sequence widgets. So there should be different renderer accessors used for either.
The serialize method can be considered the renderer in this case, and this works, so we should probably avoid complicating things by adding a further level of indirection.
That said, if you are willing to prepare a proof of concept or make a pull request, we could revisit this issue.
Thanks a lot for your constant and useful participation.
@rbu can you elaborate why this feature would useful? I'm struggling to comprehend what has been discussed. I'm not sure what you mean by "extending a widget", and what is the root of the problem. Thanks for any clarification.