Extension:TreeAndMenu: Difference between revisions

From mediawiki.org
(Undo revision 492591 by (talk))
No edit summary
Line 1: Line 1:
{{Extension code in wiki}}
|name = TreeAndMenu
|name = TreeAndMenu

Revision as of 20:37, 5 February 2012

MediaWiki extensions manual
OOjs UI icon advanced-invert.svg
Release status: stable
Implementation Parser function
Description Creates dynamic dropdown and folder tree menus
Author(s) Nad, Zven
Latest version 1.2.4 (2012-01-09)
MediaWiki all versions
License GPL
  • $wgTreeAndMenuSidebarMenuPage
  • $wg@
  • $wgTreeAndMenuSidebarMenuHeading
  • $wgTreeAndMenuPersistIfId
Translate the TreeAndMenu extension if it is available at translatewiki.net

Extension:TreeAndMenu allows for dynamic dropdown menus, merging the best JavaScript dynamic menu (Sons of Suckerfish) into the TreeView code base, and renamed the extension from TreeView5 to TreeAndMenu.

This extension makes bullet lists into folder trees or dynamic drop-down menus. It has been tested successfully on MediaWiki versions 1.6.10, 1.8.4, 1.9.3, 1.10.2, 1.11.0, 1.12.0, 1.13.1, 1.14.0, 1.15.0, 1.16.5, 1.17.1, 1.18.0. And is known to work on Safari, IE6, IE7 and all Mozilla based browsers such as Firefox and SeaMonkey.

Note: If you're using MediaWiki 1.16 or less, then make sure you're using the latest TreeAndMenu version that is less than 2.0.0. If using MediaWiki 1.17 or greater then you should use the latest version TreeAndMenu which is at least version 2.0.0. MediaWiki has undergone a lot of changes in recent versions, if you have trouble running this extension, please enter useful information in the talk page, don't spam this documentation page. It takes a lot of work to write code and it's all done for free, so can take a long time to get the extension up to date with the current versions of MediaWiki.


Screenshot showing a tree on the left and a dynamic drop-down menu on the right


Create a TreeAndMenu folder in your extensions directory. Download the latest snapshot and extract it to your extensions directory. Then include it in your LocalSettings.php file as in the following example.


The TreeAndMenu on SVN also contains the dTree tree menu component, but you may want to check source from www.destroydrop.com/javascripts/tree in case a later version is available.

Ensure that all the files you've downloaded and saved are accessible by the webserver.

Finally, add the CSS stylesheet to your installation by modifying/creating your MediaWiki:Common.css article (note that's an article not a file). Otherwise, your menu will look just like a nested list.


Tree-views are created by surrounding a normal nested bullet list within the following couple of examples:

**Another sub-item
**Another sub-item

**Another sub-item
**Another sub-item

{{#tree:id=siteTree|openlevels=1|root=Organic Design|
**Another sub-item

The second tree example uses a tree-view parameter called openlevels to make the tree expand by default to one level. Currently the parameter can only be supplied on a per-tree basis, to have different branches open to different depths requires multiple tree definitions. The second argument is used to add a label to the tree (as shown on the tree at http://www.organicdesign.co.nz).

Parser function parameters

  • root= The content of the root node. Later this will also affect how sub-trees work
  • id= A tree must be given a unique identifier if you want its state to be persistent
  • openlevels= Opens a tree to specified depth by default
  • open= equals top | bottom
  • close= equals top | bottom


  • root
  • folder
  • folderOpen
  • node
  • empty
  • line
  • join
  • joinBottom
  • plus
  • plusBottom
  • minus
  • minusBottom
  • nlPlus
  • nlMinus

CSS styles for trees

The style of trees is defined using CSS by adding the rules to your MediaWiki:Common.css article. An example set of CSS rules have been included below which are those supplied with the dTree source code.

| dTree 2.05 | www.destroydrop.com/javascript/tree/ |
| Copyright (c) 2002-2003 Geir Landr�               |

.dtree {
        font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
        font-size: 11px;
        color: #666;
        white-space: nowrap;
.dtree img {
        border: 0px;
        vertical-align: middle;
.dtree a {
        color: #333;
        text-decoration: none;
.dtree a.node, .dtree a.nodeSel {
        white-space: nowrap;
        padding: 1px 2px 1px 2px;
.dtree a.node:hover, .dtree a.nodeSel:hover {
        color: #333;
        text-decoration: underline;
.dtree a.nodeSel {
        background-color: #c0d2ec;
.dtree .clip {
        overflow: hidden;
        padding-bottom: 1px;

CSS styles for menus

The look and layout dynamic menus can also be configured purely in CSS by adding the rules to your MediaWiki:Common.css article. Below is an example which was used in the example at OrganicDesign:Extension talk:TreeAndMenu.php.

* Dynamic menus
.dmenu { width: 180px }

.dmenu, .dmenu ul { /* all lists */
padding: 0;
margin: 0;
list-style: none;
.dmenu .selflink { font-weight: normal; }

.dmenu li { /* all list items */
position: relative;
float: left;
width: 172px;
padding: 4px;
z-index: 100;

.dmenu li,
.dmenu li.even a,
.dmenu li.odd a,
.dmenu li li,
.dmenu li li a,
.dmenu li li li,
.dmenu li li li a {
color : black;

.dmenu li li { margin: 0; }

.dmenu li ul { /* second-level lists */
position: absolute;
left: -999em;
z-index: 101;
border: 1px solid #ccc;

.dmenu li ul ul { /* third-and-above-level lists */
position: absolute;
left: -999em;
margin: 0 0 0 0;
z-index: 102;

.dmenu li.odd .submenu {
width: 12px;
height: 15px;
float: right;
background: url(/common/images/rarr-lt.png) 0 3px no-repeat;
.dmenu li.even .submenu {
width: 12px;
height: 15px;
float: right;
background: url(/common/images/rarr-dk.png) 0 3px no-repeat;
.dmenu li.odd {
	background: #E9EDF4;
	border: 2px solid #E9EDF4;
.dmenu li.even {
	background: #D0D8E8;
	border: 2px solid #D0D8E8;

.dmenu li:hover, .dmenu li.sfhover {
	border: 2px solid #385D8A;

.dmenu li:hover ul ul,
.dmenu li:hover ul ul ul,
.dmenu li.sfhover ul ul,
.dmenu li.sfhover ul ul ul {
left: -999em;

.dmenu li:hover ul,
.dmenu li li:hover ul,
.dmenu li li li:hover ul,
.dmenu li.sfhover ul,
.dmenu li li.sfhover ul,
.dmenu li li li.sfhover ul { /* lists nested under hovered list items */
left: 182px;
top: -3px;

Dynamic Trees and Menus

You can use transclusion to embed the content of trees from other articles, or dynamically manipulate the content. For example a DPL query could generate the body of a treeview statement. Articles containing trees should not have any whitespace above the wikitex markup specifying the parser function tree {{#tree:...}} otherwise the rendering can fail. Here's a dynamic example used in conjunction with Extension:Simple Security which creates a tree which exhibits some links that are only visible for sysops.

*[[Main Page]]
*[[Special:Recentchanges|Recent changes]]
  • Note: The tree-view code will remove any empty items so they can work conditionally like this.

Here's another example of a dynamic tree using the DPL extension to make a tree which draws its items from all the articles in the foo category.

*Articles in [[:Category:Foo]]

The query uses some DPL parameters to ensure that the results are preceded by double asterisks so that the items can appear inside the root node. See also this example for a more advanced use of DPL with tree-view to create a menu which contains two levels of outgoing links from a given page, or incoming pages to a given page.


Trees can be transcluded within other trees so we can define large trees from structures of smaller trees. Such sub-trees are defined using the following syntax:

***Sub item1

In this example, an article called Tree2 is transcluded as an item in Tree1. Tree2 is defined as a normal tree starting at root which can be used elsewhere in the normal way. The tree-view code matches nested trees and adjusts them to the appropriate depth for them to seemlessly integrate into single whole tree. The class and other attributes of sub-trees are ignored and the whole tree renders in accord with the attributes of the root tree.

Adding a treeview to the sidebar (if using monobook skin)

One of the most common uses for the treeview is to replace the links in the sidebar with a tree. The following code can be added to your LocalSettings.php file after the treeview include line which allows a wiki article to be added to the sidebar below the toolbox. This example adds the article named "MediaWiki:NavTree" which can contain your tree, or any other wikitext content you'd like in your sidebar such as user-specific bookmarks etc.

$wgHooks['MonoBookTemplateToolboxEnd'][] = 'wfNavTree';
function wfNavTree() {
        global $wgUser, $wgTitle, $wgParser;
        if ( is_object( $wgParser ) ) $psr =& $wgParser; else $psr = new Parser;
        $opt = ParserOptions::newFromUser( $wgUser );
        $nav = new Article( Title::newFromText( 'NavTree', NS_MEDIAWIKI ) );
        $out = $psr->parse( $nav->fetchContent( 0, false, false ), $wgTitle, $opt, true, true );
        echo '</ul></div>' . $out->getText();
        return true;

If you want to also remove the existing toolbox and navigation links, you can use CSS as in the following example (the toolbox needs to be removed slightly more specifically than navigation because the tree-view renders inside its main div element). CSS rules can be added to the MediaWiki:Common.css article, remember to refresh your browser to force the CSS to reload to see the changes take effect.

#p-navigation, #p-tb h5, #p-tb .pBody { display:none }
  • You may also need to add width: 100%; in your tree-view CSS to fix IE alignment issues when adding to the sidebar.

Adding a treeview to the sidebar (if using Vector skin)

Change the first line of the above code to:

$wgHooks['SkinTemplateToolboxEnd'][] = 'wfNavTree';

And render using IE and Safari, the toolbox was still showing. This fixes it.

#p-navigation, #p-tb h5, #p-tb div.body { display:none !important; }

Sites using TreeAndMenu

Here's a list of wikis with the tree view installed:

Change Log

Refer to SVN for changes to TreeAndMenu.php, or to a historical revision of this article for change log and other information about older versions of this extension.

See also