[Grok-dev] Spotlight on: megrok.navigation

Jeroen Michiel jmichiel at yahoo.com
Tue Feb 2 05:52:28 EST 2010

Spotlight on...

megrok.navigation is a package for easily creating nested menus for all
sorts of use cases:
* Global menu items: links to external sites for instance
* Site menuitems: fixed links to views of your site (e.g a home link)
* Context menus: everything you can do on the current context (replacement
for IBrowserMenu)
* Content menus: direct links to objects in you site. E.g. a 'products' menu

Menus are viewletmanagers, and can thus be rendered with the provider:<name>
TALES expression wherever and whenever needed.
Likewise, menu items are viewlets, that also support ordering.
This also means that, should you have yet another use case that
megrok.navigation doesn't directly solve, you can always register an extra
viewlet to a menu and implement your usecase there.

The used page templates can easily be overriden with megrok.pagetemplate.

You define a menu like this:

    >>> class Navigation(navigation.Menu):
    ...     grok.name('navigation')
    ...     navigation.globalmenuitem('http://grok.zope.org', 'Grok!')

You define a site menu on a site view like this:

    >>> class Index(grok.View):
    ...     grok.context(MySite)
    ...     navigation.sitemenuitem(Navigation, 'Home')

Defining a context menu and associating a view to it, is done like that:

    >>> class Actions(navigation.ContextMenu):
    ...     grok.name('context-menu')

    >>> class FooIndex(grok.View):
    ...     grok.context(IFoo)
    ...     navigation.menuitem(Actions, 'Details', order=0)

and nested content menus like this:

    >>> class ProductMenu(navigation.ContentMenu):
    ...     navigation.contentsubmenu('series-menu')
    ...     grok.name('products')
    ...     def getContent(self):
    ...         return [x for x in site.values() if IProduct.providedBy(x)]    
    ...     def getTitle(self, obj):
    ...         return obj.name    

    >>> class SeriesMenu(navigation.ContentSubMenu):
    ...     grok.name('series-menu')
    ...     def getTitle(self, obj):
    ...         return obj.name    

If you want to add the context and content menus to the original navigation
menu, you rewrite it to

    >>> class Navigation(navigation.Menu):
    ...     grok.name('navigation')
    ...     navigation.globalmenuitem('http://grok.zope.org', 'Grok!')
    ...     navigation.submenu('context-menu', 'Actions')
    ...     navigation.submenu('products', 'Products')

Rendering this menu just needs a "structure provider:navigation" TALES

I wrote this package because I didn't find a package out there that
implemented all my use cases in an easy way (especially content menus).
There's megrok.menu, but I found it somehow lacking in functionality and a
bit counterintuitive. It could be 'abused' to show site and global menu
items, but I don't like 'abuse'. And I don't know for content menus. There's
also z3c.menu.ready2go, but that relies on z3c.template, which brought it's
own problems with it. I know Souheil is working on dolmen.menu, but there's
no official releas yet, and I doubt it will include content menus.

Possible extensions: a BrowserMenu class that renders existing
IBrowserMenus, if there is a demand.

For further docs, see http://pypi.python.org/pypi/megrok.navigation/

Any comments, remarks, ideas are appreciated!
View this message in context: http://old.nabble.com/Spotlight-on%3A-megrok.navigation-tp27418502p27418502.html
Sent from the Grok mailing list archive at Nabble.com.

More information about the Grok-dev mailing list