Skip to main content

A simple menu system for Zope3

Project description

This package provides a simple menu implementation based on viewlets.

Simple Menu

The package provides a simple menu implementation which allows you to implement simply menus based on content providers and viewlets.

Right now there are some SimpleMenuItem menu item implementations and a tabbed menu with tab/tab-item and action/action-item located in this package.

Let’s see what this means.


>>> from zope.viewlet.interfaces import IViewlet
>>> from zope.viewlet.interfaces import IViewletManager

Let’s create a menu which means we define a viewlet manager interface:

>>> class IMenu(IViewletManager):
...     """Menu viewlet manager."""

You can then create a viewlet manager using this interface now:

>>> from zope.viewlet import manager
>>> Menu = manager.ViewletManager('left', IMenu)

Now we have to define a context:

>>> import zope.interface
>>> from import contained
>>> from import IContained
>>> class Content(contained.Contained):
...     zope.interface.implements(IContained)
>>> root['content'] = Content()
>>> content = root['content']
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> class View(contained.Contained):
...     zope.interface.implements(IBrowserView)
...     def __init__(self, context, request):
...         self.__parent__ = context
...         self.context = context
...         self.request = request
>>> view = View(content, request)
>>> menu = Menu(content, request, view)

So initially no menu get rendered:

>>> menu.update()
>>> menu.render()

But now we register a context menu item for the IMenu:

>>> import zope.component
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> from import ContextMenuItem
>>> class MyLocalLink(ContextMenuItem):
...     __name__ = u'MyLocalLink'
...     urlEndings = 'myLocal.html'
...     viewURL = 'myLocal.html'
>>> # Create a security checker for viewlets.
>>> from import NamesChecker, defineChecker
>>> viewletChecker = NamesChecker(('update', 'render'))
>>> defineChecker(MyLocalLink, viewletChecker)
>>> zope.component.provideAdapter(
...     MyLocalLink,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, IMenu),
...     IViewlet, name='MyLocalLink')

Now see what we get if the IMenu viewlet manager get used:

>>> menu.update()
>>> print menu.render()
<a href=""


Now we create a tabbed menu called MasterMenu:

>>> class IMasterMenu(IViewletManager):
...     """Master menu viewlet manager."""

Let’s create a viewlet manager using this interface and the TabMenu as base class:

>>> from import TabMenu
>>> MasterMenu = manager.ViewletManager('masterMenu', IMasterMenu,
...                                     bases=(TabMenu,))

We use the same context, request and view like before:

>>> masterMenu = MasterMenu(content, request, view)

So initially no menu get rendered:

>>> masterMenu.update()
>>> masterMenu.render()

Now we register a menu tab which is also a viewlet manager:

>>> from zope.browserpage import viewpagetemplatefile
>>> from import ITab
>>> from import Tab
>>> class MyTabs(Tab):
...     template = viewpagetemplatefile.ViewPageTemplateFile('')
>>> myTabs = MyTabs(content, request, view)

Also here, initially no tab get rendered:

>>> myTabs.update()
>>> myTabs.render()

Now we register a menu action which is also a viewlet manager:

>>> from import IAction
>>> from import Action
>>> class MyActions(Action):
...     template = viewpagetemplatefile.ViewPageTemplateFile('')
>>> myActions = MyActions(content, request, view)

Also here, initially no tab get rendered:

>>> myActions.update()
>>> myActions.render()

After setup the TabMenu, Tab and Action viewlet managers, we start to register a tab menu item:

>>> from import TabItem
>>> class MyTab(TabItem):
...     __name__ = u'MyTab'
...     url = 'myTab.html'
...     selectedViewNames = ['myTab.html']
>>> tabChecker = NamesChecker(('update', 'render', 'css', 'selected'))
>>> defineChecker(MyTab, tabChecker)
>>> zope.component.provideAdapter(
...     MyTab,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, ITab),
...     IViewlet, name='MyTab')

Now see what we get if the tab viewlet manager get rendered:

>>> myTabs.update()
>>> print myTabs.render()
<div class="tabMenu">
  <span class="inactive-menu-item">
  <a href="myTab.html">MyTab</a>

After showing how a tab menu item get used, we will register a menu action item.

>>> from import ActionItem
>>> class MyAction(ActionItem):
...     __name__ = u'MyAction'
...     title = 'myAction'
>>> actionChecker = NamesChecker(('update', 'render', 'title'))
>>> defineChecker(MyAction, actionChecker)
>>> zope.component.provideAdapter(
...     MyAction,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, IAction),
...     IViewlet, name='MyAction')

Now see what we get if the action viewlet manager get used:

>>> myActions.update()
>>> print myActions.render()
<div class="actionMenuWrapper">
  <ul class="actionMenu">
    <li class="inactive-menu-item">
      <a href="">
<div class="clearActionMenu" />


0.6.0 (2010-07-14)

  • Depend on zope.browserpage instead of as needed code has been moved there.

  • No longer use deprecated zope.testing.doc[unit]test, use python’s doctest instead.

0.5.2 (2009-11-19)

  • Removed dependency.

0.5.1 (2008-01-25)

  • Bug: Updated meta-data and documentation.

0.5.0 (2008-01-21)

  • Initial Release.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution (9.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page