[Grok-dev] Using five.grok to add new viewlets

Vincent Fretin vincent.fretin at gmail.com
Thu Jan 15 02:37:27 EST 2009


On Mon, Jan 12, 2009 at 10:14 AM, Sylvain Viollon <sylvain at infrae.com> wrote:
>  We can simplify your code if you want:
>
>
> from five import grok
> from plone.app.layout.viewlets.interfaces import IPortalHeader
> from zope.interface import Interface
>
> # Apply on everything.
> grok.context(Interface)
> # Use templates directory to search for templates.
> grok.templatedir('templates')
>
> class AdViewlet(grok.Viewlet):
>    grok.baseclass()
>    # Set the name of the searched template to adviewlet. By default
>    # it's the class name, but doing that it will that name, as well
>    # for inherited class (and the class name of those classes).
>    grok.template('adviewlet')
Oh, I didn't know this grok.template directive will be inherited.
Interesting. I'll look the tests you pointed out.
I tried this, but I have a traversal error. I'll do some research this
weekend and send a new mail with feedback.

>
>    def getBanner(self):
>        # do a search with self.context and self.request
>        # and return an image object
>
>
>
> class AdTopViewlet(AdViewlet):
>    grok.viewletmanager(IPortalHeader)
>
> class AdHomeViewletManager(grok.ViewletManager):
>    grok.name('admanager.home')
>
> class AdHomeViewlet(AdViewlet):
>    grok.viewletmanager(AdHomeViewletManager)
>
>
> class AdRightTopViewletManager(grok.ViewletManager):
>    grok.name('admanager.righttop')
>
>
> # Set the viewletmanager for all viewlets of that file to
> # AdRightTopViewletManager. This one will be used unless you specify an
> # another viewletmanager directive at the class level.
>
> grok.viewletmanager(AdRightTopViewletManager)
>
> class AdRightTopViewlet1(AdViewlet):
>    pass
>
> class AdRightTopViewlet2(AdViewlet):
>    pass
>
> class AdRightTopViewlet3(AdViewlet):
>    pass
Yes, I knew this one. In my real code I have two viewletmanager in the
same module.
But you right, I should put this example in my blog post.

Thanks for your reply Sylvain.
-- 
Vincent Fretin


More information about the Grok-dev mailing list