Hi,

I have some code where I try to get a named template and render it and
if it fails I render a default template instead.  But this fails,
presumably because the __call__ method of NamedTemplate does not
return anything.  From zope/formlib/namedtemplate.py (Zope 2.10):

class NamedTemplate(object):

    def __init__(self, name):
        self.__name__ = name

    def __get__(self, instance, type=None):
        if instance is None:
            return self
        return component.getAdapter(instance, INamedTemplate, self.__name__)

    def __call__(self, instance, *args, **kw):
        self.__get__(instance)(*args, **kw)

I seems strange to me that __call__ does not actually return
anything.  Is it just me?

Now, I have some other code where NamedTemplate is working just fine,
so there is probably nothing wrong with this __call__ method.  So can
anybody tell me what is wrong with my following code then?

class Renderer(base.Renderer):
    default_template = ViewPageTemplateFile('vocabularyportlet.pt')

    def render(self):
        """Render a named template or the default template.
        """
        template = NamedTemplate("test.vocabulary")
        try:
            output = template(self)
        except ComponentLookupError:
            output = None
        # Even if the component lookup works, output can be None.
        if output is None:
            output = self.default_template()
        return output

The line "output = template(self)" fails with a ComponentLookupError
unless a named template with the name test.vocabulary exists.  But
when that template is found, output is still None.

In case you want to see the entire code, this is in a Plone portlet
that I am developing:
http://svn.plone.org/svn/collective/collective.portlet.vocabulary/trunk/collective/portlet/vocabulary
 

And actually, the tests that I added pass when I change the
NamedTemplate.__call__ method to return the value that it just got.

So should that method be changed or is there a different way to do this?

Thanks,

-- 
Maurits van Rees | http://maurits.vanrees.org/
            Work | http://zestsoftware.nl/
"This is your day, don't let them take it away." [Barlow Girl]

_______________________________________________
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )

Reply via email to