[Grok-dev] Viewlets

jure jcerjak at termitnjak.si
Wed Jul 14 20:31:50 EDT 2010

On 07/14/2010 04:32 PM, Daniel Lyons wrote:
> Hi again,
> I'm still working through _Grok 1.0 Web Development_ but also trying
> to apply the lessons in a small app that does the same thing as the
> todo list app, but structured somewhat differently. In the book,
> pretty much everything winds up in the app.py file, which seems fine
> for a tutorial, but as I rewrite it I have been trying to break things
> down into app.py, todolist.py and todo.py but I've run into a
> connundrum with handling viewlets. So I now think I may have
> completely lost how viewlets work in my mind.
> In general, how does one use viewlets to structure an application? I
> have the master template in app_templates/master.pt but it seems as
> though to use this template the view must be in app.py rather than
> (say) todolist.py. Also, I'm wondering why it's strictly necessary to
> have both a view and viewlet class. Seems like it's going to lead to
> twice as many classes, one for each "page" I want to have and one to
> embed that page in a viewlet. Is that right? Am I misunderstanding
> something?
> I did download and go through the Paleosoft sample code, but none of
> those projects are using viewlets, so it was of somewhat limited
> utility. Is there a medium-sized Grok project I can refer to that does
> things right and uses viewlets?
> Thanks again,


i had a quick look at how the layout is done in the Grok book, but I've 
done it
slightly differently in my project, the megrok.layout way, so be sure to 
check the
http://pypi.python.org/pypi/megrok.layout/0.9 for more info.

The procedure:

1. Define your viewlet managers and viewlets

2. Define a layout for your application

import grok
from zope.interface import Interface


class MyLayout(Layout):
     title = u"My layout"

3. in the templates directory create a master layout template mylayout.pt
    and the viewlet templates. My layout template looks something like this:

<tal:block tal:content="structure provider:headslot" />
<div id="header">
<tal:header tal:content="structure provider:header" />
<div id="content" tal:content="structure view/content" />
<div id="footer">
<tal:footer tal:content="structure provider:footer" />

So, we have a couple of viewlet managers which render the asigned viewlets,
and the <div id="content" tal:content="structure view/content" /> part,
which renders the content from your views and forms.

4. For this magic to work, you have to use layout aware view components:
  instead of grok.View use megrok.layout.Page, instead of grok.AddForm
  use megrok.layout.AddForm, etc.

You can put the layout code and the templates wherever you like, my folder
structure lookes like this(I hate large modules, with everything crammed 
in :) ):


I hope it makes sense now(and that my newbie explanation is correct :)),
I can also send you the source if you'd like..



More information about the Grok-dev mailing list