Hello,

I'm doing a project where different instances of the same content type might need different templates. All the instances in a given folder will share a template. The manager of the site should be able to change the template through the ZMI. I want the TTW templates to have the same namespace keys as a ViewPageTemplateFile, so they can be used interchangeably by a View Class.

What I have done for a first cut is make an adapter for ZPTPage => ViewPageTemplateFile. (See below.) The two Template classes actually implement the same interface, but the namespace available to TALES within the template is quite different.

My general question is, is this a reasonable way to proceed? It works OK with Benji York's Hello World example; in particular the 'view' and 'context' namespaces seem to be complete. But Hello World doesn't exercise the code much. Is there another pattern I should be using instead? Have I overlooked a perfectly good solution that's already out there?

Wade Leftwich
Ithaca, NY

########
# View class that uses the adapter

class MessageView(object):
    """A webpage saying hello
    """
    def message(self):
        return '%s %s!' % (self.context.greeting, self.context.subject)

class KustomView(MessageView):
    """Pick up a template from parent container if available;
    if not, use the filebased one.
    """
    def __call__(self):
        template = self.context.__parent__.get('kustom.pt')
        if template is not None:
template = getAdapter(template, IPageTemplateSubclassing, "zptViewAdapter")
        else:
            template = ViewPageTemplateFile('stock.pt')

        return template(self)

##########
# The adapter

class ZPTViewAdapter(ZPTPage):
    """Adapt a ZPTPage instance to set up its namespace
    like zope.app.pagetemplate.viewpagetemplatefile.ViewPageTemplateFile,
    so it can be called by a View Class.
    """
    implements(IPageTemplateSubclassing)
    adapts(ZPTPage)

    def __init__(self, ob):
        self.ob = ob

    def pt_getContext(self, instance, request, **kw):
        """instance is a View component"""
namespace = super(ZPTViewAdapter, self).pt_getContext(instance, request, **kw)
        namespace['nothing'] = None
        namespace['template'] = self.ob
        namespace['container'] = self.ob.__parent__
        namespace['request'] = request
        namespace['view'] = instance
        namespace['context'] = context = instance.context
        namespace['views'] = ViewMapper(context, request)
        namespace['options'] = kw
        print 'ZPTPage namespace', namespace.keys()
        return namespace

    def __call__(self, instance, *args, **keywords):
        namespace = self.pt_getContext(
            request=instance.request,
            instance=instance, args=args, options=keywords)
        debug_flags = instance.request.debug
        s = self.ob.pt_render(
            namespace,
            showtal=getattr(debug_flags, 'showTAL', 0),
            sourceAnnotations=getattr(debug_flags, 'sourceAnnotations', 0),
            )
        response = instance.request.response
        if not response.getHeader("Content-Type"):
            response.setHeader("Content-Type", self.ob.content_type)
        return s
_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to