Content - Menus
Introduction
Menus are a hierarchical list of links built up using ContentMenu
objects. You can define several different menus (such as main, footer, etc.) so that admins can gain edit access to the contents of each menu on your site.
Defining available menus
Take a look at the default content.conf.ini
file. You’ll notice that the types mainmenu
and footer
are already predefined:
[contentmenu] # This is a comma-separated list of available menu types. Create a section below for each type. contentmenu_types = mainmenu,footer
The name for each of these menus is defined in the corresponding contentmenu.xx_XX.lang.ini
language file. The name is only visible to admins, so make sure it is descriptive for their purposes and is available in each of the admin languages:
# Configure the name of each menu type here menu_contentmenu_mainmenu = Main menu menu_contentmenu_footer = Footer
Setting up menus in the admin
Once you have set up the menu types the proper options will become visible under the content section of your admin panel. You should set up some links here before you move on to creating the templates.
Displaying menus
To display the menu, simply use the {% contentmenu 'mainmenu' %}
tag.
Menus will try to look for a contentmenu/_type_name.html
menu-specific template, so in the example of main menu this will be contentmenu/_mainmenu.html
. If such a template is not found, it will revert to the default at contentmenu/_default.html
.
Take a look at the _default.html implementation to see how to build a hierarchical menu and go from there to build your own. Or you can simply use the default and style it according to your needs.
ContentMenu model and properties
Menu items are stored as a hierarchy of ContentMenu
objects. Each object has an unlimited amount of children
(zajFetcher) and/or a single parent
(ContentMenu). Each object also has either a a pointer to a contentpage
(ContentPage), an internal link
(string, relative to baseurl), or an external externallink
. You can reach these data items via the standard data property, so an example would be {{contentmenu.data.parent}}
.
There are also some cached and smart properties that can help you when you are creating a custom template. These are:
published
– boolean – this is true if the associated ContentPage is published.parents
– ContentMenu[] – an array of ContentMenu objects that are the parents of this item starting with the top level item first. This is useful for generating breadcrumbs.child_count
– integer – The number of child ContentMenu items.url
– string – The full url of the associated ContentPage or the manually set link.identifier
– string – You can use this for custom classes or other uses. Any custom string id.active
– boolean – If true, it means the currently displayed ContentPage is the associated ContentPage or if the currently displayed controller is the current url according to routing.hasactivechild
– boolean – If true, it means the current item has a child ContentMenu which is currently active.hasactiveparent
– boolean – If true, it means the current item has a parent ContentMenu which is currently active.
Custom templates
In certain situations you will want the same menu content to show up with different markup. For these instances, you can use the second parameter of the contentmenu tag: {% contentmenu 'mainmenu' 'contentmenu/_mainmenu_alternate.html' %}
.
With this code, instead of loading up the usual _mainmenu.html it will instead load _mainmenu_alternate.html.