PageTemplateLookup

Looking up and re-using page templates

In z3c.form.form.BaseForm? page templates are looked up using the zope.pagetemplate.interface.IPageTemplate? interface:

def render(self):
    '''See interfaces.IForm'''
    # render content template
    if self.template is None:
        template = zope.component.getMultiAdapter((self, self.request),
            IPageTemplate)
        return template(self)
    return self.template()

This is very useful and allows separation of the page template classes, but makes it laborious to replace the page template with another method of rendering. For example, to optimize speed by replacing a page template rendering with python rendering would (strictly) require something like this:

class MyWidget(object):

    implements(IMyPageTemplate)

    def __init__(self, view, request):
        self.view = view
        self.request = request
        self.macros = {}

    def __class__(self, *args, **kw):
        return u"<h1>My Widget</h1><p>%s</p>" # self.context.content

    def pt_edit(source, content_type):
        raise NotImplementedError()

    def pt_errors(namespace):
        raise NotImplementedError()

    def read():
        raise NotImplementedError()

Much of the above is clearly pointless as all that is required from the adapter is a __call__ method that returns a unicode string.

Thus it is proposed to break __call__(*args, **kw): out of IPageTemplate? into it's own interface from which IPageTemplate? inherits. z3c.form can then adapt to the smaller interfaces, page templates can be optimized easily.

The proposed name of the new interface is IRenderer? (grepping what was the zope3 trunk turns up no results).

Issues

  • Where should the new interface live?

Side Issues

  • The new interface is clearly similar to zope.publisher.interfaces.browser.IBrowserPage?'s __call__ method (i.e. both take *args, **kw and return a unicode string). Are there more interfaces that conform to this out there?

Disadvantages

  • YAI (Yet Another Interface)



( 97 subscribers )