Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Widget should own its renderer #147

Open
rbu opened this Issue Feb 12, 2013 · 2 comments

Comments

Projects
None yet
2 participants
Contributor

rbu commented Feb 12, 2013

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.

Contributor

rbu commented Feb 12, 2013

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.

Member

cguardia commented Mar 20, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment