Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

122 lines (85 sloc) 6.686 kb

How the menu system works

Basic concepts

Registration

The menu system isn't monolithic. Rather, it is composed of numerous active parts, many of which can operate independently of each other.

What they operate on is a list of menu nodes, that gets passed around the menu system, until it emerges at the other end.

The main active parts of the menu system are menu generators and modifiers.

Some of these parts are supplied with the menus application. Some come from other applications (from the cms application in django CMS, for example, or some other application entirely).

All these active parts need to be registered within the menu system.

Then, when the time comes to build a menu, the system will ask all the registered menu generators and modifiers to get to work on it.

Generators and Modifiers

Menu generators and modifiers are classes.

Generators

To add nodes to a menu a generator is required.

There is one in cms for example, which examines the Pages in the database and adds them as nodes.

These classses are subclasses of :py:class:`menus.base.Menu`. The one in cms is :py:class:`cms.menu.CMSMenu`.

In order to use a generator, its :py:meth:`get_nodes` method must be called.

Modifiers

A modifier examines the nodes that have been assembled, and modifies them according to its requirements (adding or removing them, or manipulating their attributes, as it sees fit).

An important one in cms (:py:class:`cms.menu.SoftRootCutter`) removes the nodes that are no longer required when a soft root is encountered.

These classes are subclasses of :py:class:`menus.base.Modifier`. Examples are :py:class:`cms.menu.NavExtender` and :py:class:`cms.menu.SoftRootCutter`.

In order to use a modifier, its :py:meth:`modify()` method must be called.

Note that each Modifier's :py:meth:`modify()` method can be called twice, before and after the menu has been trimmed.

For example when using the {% show_menu %} templatetag, it's called:

This corresponds to the state of the nodes list before and after :py:meth:`menus.templatetags.menu_tags.cut_levels()`, which removes nodes from the menu according to the arguments provided by the templatetag.

This is because some modification might be required on all nodes, and some might only be required on the subset of nodes left after cutting.

Nodes

Nodes are assembled in a tree. Each node is an instance of the :py:class:`menus.base.NavigationNode` class.

A NavigationNode has attributes such as URL, title, parent and children - as one would expect in a navigation tree.

Warning

You can't assume that a :py:class:`menus.base.NavigationNode` represents a django CMS Page. Firstly, some nodes may represent objects from other applications. Secondly, you can't expect to be be able to access Page objects via NavigationNodes.

How does all this work?

Tracing the logic of the menu system

Let's look at an example using the {% show_menu %} templatetag. It will be different for other templatetags, and your applications might have their own menu classes. But this should help explain what's going on and what the menu system is doing.

One thing to understand is that the system passes around a list of nodes, doing various things to it.

Many of the methods below pass this list of nodes to the ones it calls, and return them to the ones that they were in turn called by.

Don't forget that show_menu recurses - so it will do all of the below for each level in the menu.

Jump to Line
Something went wrong with that request. Please try again.