Re: Conditionally render viewlet

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

Thanks for replying but unfortunately your suggestion didn't work:

I tried:

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

The viewlet still rendered even though theres no index_left.

Then I checked the documentation here
http://pypi.python.org/pypi/zope.viewlet, specifically the
'ConditionalViewletManager' section where it states that Viewlets have
an 'available' attribute (not a method), so I tried setting available
=3D False,  but still the viewlet rendered.

Any further suggestions?

Jon



On 8 June 2011 10:39, Antonin AMAND <[email protected]> wrote:
> 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 re=
ndering
>> =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
> _______________________________________________
> silva-dev mailing list
> silva-dev-IAPFreCvJWM6s/[email protected]
> https://lists.infrae.com/mailman/listinfo/silva-dev
>



-- =

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