Back to index

plone3  3.1.7
_named.py
Go to the documentation of this file.
00001 import new
00002 from Acquisition import aq_inner
00003 from zope import interface
00004 from zope.formlib import namedtemplate
00005 
00006 class NamedTemplateAdapter(object):
00007     """A named template adapter implementation that has the ability
00008     to lookup the template portion from regular traversal (intended for
00009     being able to customize the template portion of a view component
00010     in the traditional portal_skins style).
00011     """
00012 
00013     interface.implements(namedtemplate.INamedTemplate)
00014 
00015     def __init__(self, context):
00016         self.context = context
00017 
00018     @property
00019     def macros (self):
00020         return self.default_template.macros
00021 
00022     def __call__(self, *args, **kwargs):
00023         context = aq_inner(self.context)
00024         context_of_context = aq_inner(context.context)
00025         view = context.__of__(context_of_context)
00026         return self.default_template.__of__(view)(*args, **kwargs)
00027 
00028 def named_template_adapter(template):
00029     """Return a new named template adapter which defaults the to given
00030     template.
00031     """
00032 
00033     new_class = new.classobj('GeneratedClass', 
00034                              (NamedTemplateAdapter,),
00035                              {})
00036     new_class.default_template = template
00037     return new_class