[silva-dev] Conditionally render viewlet
Jonathan Bowlas
j.bowlas at ucl.ac.uk
Wed Jun 8 12:09:20 CEST 2011
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
= False, but still the viewlet rendered.
Any further suggestions?
Jon
On 8 June 2011 10:39, Antonin AMAND <antonin.amand at gmail.com> wrote:
> Hi Jon,
>
>
>> 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 ""
>
>
> 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 at lists.infrae.com
> https://lists.infrae.com/mailman/listinfo/silva-dev
>
--
Jonathan Bowlas
Web Support Officer
Media Services
University College London
Email: j.bowlas at ucl.ac.uk
More information about the silva-dev
mailing list