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

Sylvain Viollon sylvain at infrae.com
Mon Jan 12 04:14:27 EST 2009

On Wed, 31 Dec 2008 10:22:29 +0100
"Vincent Fretin" <vincent.fretin at gmail.com> wrote:

> Hello,


> I started to blog about my recent work on archgenxml:
> http://vincentfretin.ecreall.com/
> I wrote a post about my experience of using five.grok to create
> viewlets in a Plone product.
> Here it is:
> http://vincentfretin.ecreall.com/articles/using-five.grok-to-add-viewlets


> You'll see in a comment that I didn't manage to use
> grok.PageTemplateFile This work:
>     from Products.Five.browser.pagetemplatefile import
> ZopeTwoPageTemplateFile template =
> ZopeTwoPageTemplateFile('templates/adviewlet.pt') def render(self):
>         return self.template()
> And I tried various things, I didn't manage to use
> grok.PageTemplateFile. My last try was:
>     template = grok.PageTemplateFile('templates/adviewlet.pt')
>     def render(self):
>         self.template.render(self)
> But this don't work.
> How it is supposed to work? How do you render the template if you use
> grok.PageTemplateFile?

  Well, it's far more easy than that you think. Templates work exactly
the same for viewlets than for view. I think maybe you have some
example in the functional tests:

  http://svn.zope.org/five.grok/trunk/src/five/grok/ftests/viewlet/ and

  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.
# Use templates directory to search for templates.

class AdViewlet(grok.Viewlet):
    # 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).

    def getBanner(self):
        # do a search with self.context and self.request
        # and return an image object

class AdTopViewlet(AdViewlet):

class AdHomeViewletManager(grok.ViewletManager):

class AdHomeViewlet(AdViewlet):

class AdRightTopViewletManager(grok.ViewletManager):

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


class AdRightTopViewlet1(AdViewlet):
class AdRightTopViewlet2(AdViewlet):

class AdRightTopViewlet3(AdViewlet):

   Best regards,

   Sylvain Viollon

Sylvain Viollon -- Infrae
t +31 10 243 7051 -- http://infrae.com
Hoevestraat 10 3033GC Rotterdam -- The Netherlands
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://mail.zope.org/pipermail/grok-dev/attachments/20090112/b567e9df/attachment-0001.bin 

More information about the Grok-dev mailing list