[silva-dev] Conditionally render viewlet

Jonathan Bowlas j.bowlas at ucl.ac.uk
Mon Jun 13 14:43:35 CEST 2011


Hi Devs,

I've added this into my local version of silva.core.views view.py:

def isAvailable(viewlet):
    try:
        return viewlet.available
    except AttributeError:
        return True

class ConditionalViewletManager(ViewletManager):
    """A conditional viewlet manager in Silva.
    """
    grok.baseclass()
    grok.context(ISilvaObject)
    grok.implements(IViewletManager)

    def filter(self, viewlets):
      viewlets = super(ConditionalViewletManager, self).filter(viewlets)
      return [(name, viewlet) for name, viewlet in viewlets if
isAvailable(viewlet)]

Hopefully this can go into the core, although if you'd require any
modifications let me know so I can update our views.py file.

Usage in theme:

class LeftColumn(silvaviews.ConditionalViewletManager):
    """Left Content Area
    """
    pass

class LeftTopPortlet(silvaviews.Viewlet):
    """Left Column Topper Viewlet
    """
    grok.viewletmanager(LeftColumn)
    grok.name('lefttopportlet')
    available= False

class LeftNavigation(silvaviews.Viewlet):
    """Left Navigation Viewlet
    """
    grok.viewletmanager(LeftColumn)
    grok.name('leftnavigation')
    available= True

#usage with a method
class LeftPortlet(silvaviews.Viewlet):
    """Left Portlet Viewlet
    """
    grok.viewletmanager(LeftColumn)
    grok.name('leftportlet')

    @property
    def available(self):
        return self.layout.atts.ucl_left_col_include

HTH someone, thanks.

Jon

On 9 June 2011 12:41, Jonathan Bowlas <j.bowlas at ucl.ac.uk> wrote:
> I think that would be best, I'm using it a lot and it seems a bit
> excessive to include a available() method to every viewlet I want
> included. (Although I have written a separate silvaviews.Viewlet class
> called IsAvailable with has the available() method which I subclass
> when I want a Viewlet available).
>
> Cheers
>
> J
>
> On 9 June 2011 12:28, Sylvain Viollon <sylvain at infrae.com> wrote:
>> On Wed, 8 Jun 2011 15:54:05 +0200
>> Antonin AMAND <antonin at infrae.com> wrote:
>>
>>  Hello,
>>
>>> On Wed, Jun 8, 2011 at 1:37 PM, Jonathan Bowlas <j.bowlas at ucl.ac.uk>
>>> wrote:
>>> > Is there an example infrae extension or theme where the filter
>>> > method is overridden?
>>> >
>>> > J
>>>
>>> Sorry. filter method takes the viewlets as parameter.
>>>
>>> def filter(self, viewlets):
>>>    viewlets = super(LeftColumn, self).filter(viewlets)
>>>    return [(name, viewlet) for name, viewlet in viewlets if
>>> viewlet.available()]
>>>
>>>
>>
>>  If this is often used, we could add a base class into
>>  silva.core.views that contains it by default.
>>
>>  Regards,
>>
>>  Sylvain,
>>
>>
>> --
>> Sylvain Viollon -- Infrae
>> t +31 10 243 7051 -- http://infrae.com
>> Hoevestraat 10 3033GC Rotterdam -- The Netherlands
>> _______________________________________________
>> 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
>



-- 
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