deform icon indicating copy to clipboard operation
deform copied to clipboard

Widget should own its renderer

Open rbu opened this issue 12 years ago • 3 comments

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.

rbu avatar Feb 12 '13 16:02 rbu

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.

rbu avatar Feb 12 '13 16:02 rbu

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.

cguardia avatar Mar 20 '13 23:03 cguardia

@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.

stevepiercy avatar Aug 24 '20 08:08 stevepiercy