Conditionally render viewlet

Jonathan Bowlas <j.bowlas-hclig2XLE9Zaa/[email protected]> Wed, 8 Jun 2011 09:53:49 +0100
Newsgroups gmane.comp.web.zope.silva.devel
Message-ID <[email protected]>
Hi Devs

Another Viewlet question - is it possible to conditionally render a
viewlet when it has an associated template? For example I have a
viewlet that I want to render only if a certain silva doucment is
present, so I have written this:

class LeftPortlet(silvaviews.Viewlet):
    """Left Portlet Viewlet
    """
    grok.viewletmanager(LeftColumn)
    grok.name('leftportlet')
    grok.order()

    def isEnabled(self):
        """
        @return: True if the index_left is present
        """
        return hasattr(self.context, 'index_left')

    def render(self):
        """ Render viewlet only if it is enabled.
        """
        # Perform condition check
        if self.isEnabled():
            # Call parent method which performs the actual rendering
            return super(LeftPortlet, self).render()
        else:
            # No output when the viewlet is disabled
            return ""

but this returns the following error:
Traceback (most recent call last):
  File "c:\silva-2.3.1\eggs\zope2-2.12.14-py2.6-win32.egg\OFS\Application.py",
line 671, in install_product
    initmethod(context)
  File "c:\silva-2.3.1\eggs\zope2-2.12.14-py2.6-win32.egg\Products\Five\__init__.py",
line 31, in initialize
    zcml.load_site()
  File "c:\silva-2.3.1\eggs\zope2-2.12.14-py2.6-win32.egg\Products\Five\zcml.py",
line 51, in load_site
    _context = xmlconfig.file(file)
  File "c:\silva-2.3.1\eggs\zope.configuration-3.6.0-py2.6.egg\zope\configuration\xmlconfig.py",
line 649, in file
    context.execute_actions()
  File "c:\silva-2.3.1\eggs\zope.configuration-3.6.0-py2.6.egg\zope\configuration\config.py",
line 605, in execute_actions
    callable(*args, **kw)
  File "c:\silva-2.3.1\eggs\grokcore.viewlet-1.3-py2.6.egg\grokcore\viewlet\meta.py",
line 140, in checkTemplates
    has_render, has_no_render)
  File "c:\silva-2.3.1\eggs\grokcore.view-1.12.2-py2.6.egg\grokcore\view\templatereg.py",
line 129, in checkTemplates
    (component_name, factory), factory)
ConfigurationExecutionError: <class 'martian.error.GrokError'>:
Multiple possible ways to render viewlet <class
'silvatheme.uclcorporateidentity.uclcorporateidentity.LeftPortlet'>.
It has both a 'render' method as well as an associated template.
  in:
  File "c:\silva-2.3.1\src\silvatheme.uclcorporateidentity\silvatheme\uclcorporateidentity\configure.zcml",
line 6.2-6.27
    <grok:grok package="." />

Any suggestions?

Cheers

Jon



-- 
Jonathan Bowlas
Web Support Officer
Media Services
University College London
Email: j.bowlas-hclig2XLE9Zaa/[email protected]