From MediaWiki.org
(Redirected from Extension:Treeview)
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png

Release status: stable

Implementation Parser function, Skin
Description Creates dynamic dropdown and folder tree menus
Author(s) Nad, Zven
Latest version 3.0.4 (2013-02-27)
MediaWiki 1.17+
Database changes no
License GPL
Download Project page

Git [Help]
Commit history
No localisation updates are provided by translatewiki.net.

  • $wgTreeViewShowLines
Hooks used

Translate the TreeAndMenu extension if possible

Check usage and version matrix; code metrics

The TreeAndMenu extension 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.1, 1.19.0. And is known to work on Safari, IE6, IE7 and all Mozilla based browsers such as Firefox and SeaMonkey.

Installation[edit | edit source]

  • Source code on github:
  • Download a copy of the extension from github:

Open the zip file and save the TreeAndMenu folder into the extensions folder

  • Add the following code to your LocalSettings.php (at the bottom). This must be before Graphviz, if it is in use.
require_once( "$IP/extensions/TreeAndMenu/TreeAndMenu.php" );

CSS styles for trees (only required for old versions of TreeAndMenu)[edit | edit source]

  • Finally, add the CSS stylesheet below to your installation by modifying/creating your MediaWiki:Common.css page (note that's a wiki page not a file). Otherwise, your menu will look just like a nested list.
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.

The style of trees is defined using CSS by adding the rules to your MediaWiki:Common.css page. 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;

Usage[edit | edit source]

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

**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[edit | edit source]

  • 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

Images[edit | edit source]

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

Dynamic Trees and Menus[edit | edit source]

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.

Sub-trees[edit | edit source]

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 (MediaWiki 1.18+)[edit | edit source]

As of MediaWiki-1.18, I've found a good way to get wikitext into the sidebar is to attach a function to the BeforePageDisplay hook with the following content:

$wgHooks['BeforePageDisplay'][] = 'wfAddSidebarTree';
function wfAddSidebarTree( $out, $skin ) {
	global $wgUser, $wgParser;
	$opt = ParserOptions::newFromUser( $wgUser );
	$title = Title::newFromText( 'Sidebar', NS_MEDIAWIKI );
	$article = new Article( $title );
	$html = $wgParser->parse( $article->fetchContent(), $title, $opt, true, true )->getText();
	$out->addHTML( "<div id=\"wikitext-sidebar\">$html</div>" );
	return true;

This creates a CSS-addressable div element containing the parsed content from the MediaWiki:Sidebar article. You can then use some JavaScript added to your MediaWiki:Common.js to move the element into a more appropriate location in the page DOM. For example the following JavaScript snippet inserts the rendered wikitext below the site logo.

var tree = $('#wikitext-sidebar');
$('#p-logo').after( tree.html() );

Adding a treeview to the sidebar <1.18 (if using monobook skin)[edit | edit source]

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)[edit | edit source]

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[edit | edit source]

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

Opening a tree to the current page[edit | edit source]

Here's a useful snippet of JS that can be put into MediaWiki:Common.js to open the tree to the current page. It finds and .self links in the tree and opens the tree to those nodes. You need to give your tree an id attribute so you can refer to it. Then check the source and find out what the JS object of the tree is called (it will be a new dTree() statement), basically it's just the id attribute with non-alphanumerics stripped and appended to "tam". In the following example the tree being adjusted has the id attribute of "sidebar-tree".

$(document).ready( function() {
    $('#sidebar-tree .selflink').each( function() {
        tamsidebartree.openTo($(this).parent().parent().attr('id').replace(/[^0-9]/g, ''));

Change Log[edit | edit source]

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[edit | edit source]