Re: Conditionally render viewlet

Antonin AMAND <[email protected]> Wed, 8 Jun 2011 11:39:52 +0200
Newsgroups gmane.comp.web.zope.silva.devel
Message-ID <BANLkTikdHsaqxhbyXarv=X_qXqgn1ad4msH++ufCJBFtDKuKNA@mail.gmail.com>
Hi Jon,


> class LeftPortlet(silvaviews.Viewlet):
> =A0 =A0"""Left Portlet Viewlet
> =A0 =A0"""
> =A0 =A0grok.viewletmanager(LeftColumn)
> =A0 =A0grok.name('leftportlet')
> =A0 =A0grok.order()
>
> =A0 =A0def isEnabled(self):
> =A0 =A0 =A0 =A0"""
> =A0 =A0 =A0 =A0@return: True if the index_left is present
> =A0 =A0 =A0 =A0"""
> =A0 =A0 =A0 =A0return hasattr(self.context, 'index_left')
>
> =A0 =A0def render(self):
> =A0 =A0 =A0 =A0""" Render viewlet only if it is enabled.
> =A0 =A0 =A0 =A0"""
> =A0 =A0 =A0 =A0# Perform condition check
> =A0 =A0 =A0 =A0if self.isEnabled():
> =A0 =A0 =A0 =A0 =A0 =A0# Call parent method which performs the actual ren=
dering
> =A0 =A0 =A0 =A0 =A0 =A0return super(LeftPortlet, self).render()
> =A0 =A0 =A0 =A0else:
> =A0 =A0 =A0 =A0 =A0 =A0# No output when the viewlet is disabled
> =A0 =A0 =A0 =A0 =A0 =A0return ""


Viewlets have an "available" method to control whether they show or not.
Just rename isEnable to available and it should work.

By the way, with grok, either you have a render method, either you
have a template; never both.

Antonin