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

   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
> 

  Great!

> 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
http://svn.zope.org/five.grok/trunk/src/five/grok/ftests/view/. 

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

    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


   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