Skip to the content
Documentation

treemenu

No ratings yet

Summary

Fetches a subtree of nodes for the purpose of menu generation.

Usage

treemenu( path,
          node_id
          [, class_filter       ]
          [, depth_skip         ]
          [, max_level          ]
          [, is_selected_method ] )

Parameters

Name Type Description Required
path array An array of the path ($module_result.path). Yes.
node_id integer The node ID number (the root of the subtree). Yes.
class_filter array An array of classes that should be filtered. No.
depth_skip integer Number of levels that should be skipped. No.
max_level integer The max depth that should be explored. (2 by default) No.
is_selected_method string Sets whether "is_selected=TRUE" should be assigned to the parents of the current node as well. No.

Returns

A complex structure that can be used to build a menu (see below).

Description

This operator fetches a subtree of nodes and returns a complex structure (an array of hashes) that can be used to generate a menu.

If the optional "class_filter" parameter is omitted, all nodes will be fetched without filtering. If an empty array is passed, then only folder nodes (class ID = 1) will be fetched.

The default value of the "is_selected_method" parameter is "tree". This value determines that the current node and its parent nodes will be considered as selected (is_selected=TRUE). If this parameter is set to "node", only the current node will be considered as selected.

The following table shows the hash-structure for each element in the array that will be returned.

Key Type Description
id integer The ID of the node.
level integer The depth of the node.
url_alias string The URL alias of the node.
url string The system URL of the node.
text string The name of the node.
is_selected boolean TRUE if the node is currently being viewed/selected, FALSE otherwise.

Examples

Example 1 (explanatory)

Let's use a small site with the following content structure (see the screenshot) for the purpose of demonstration

Treemenu explanatory example doc

The content tree

and insert the following code into "pagelayout.tpl":

{def $mainMenu=treemenu( $module_result.path, $module_result.node_id ) }
{foreach $mainMenu as $menu}
{$menu.level} - {$menu.text}<br />
{/foreach}
{undef $mainMenu}

 

If the "Weblog" node is being viewed, then the following output will be produced:

0 - Weblog
1 - July, 29
1 - July, 14
1 - June, 25
0 - Galleries
0 - Products

 

If the "Blue flower" node is being viewed, then the following output will be produced:

0 - Weblog
0 - Galleries
1 - Misc flowers
1 - Landscape
0 - Products

 

Since the "max_level" parameter is omitted, only two levels are explored. To set the "max_level" parameter to 3, change the first line of the previous code fragment in the following way:

{def $mainMenu=treemenu( $module_result.path, $module_result.node_id, , , 3 ) }

 

This will produce the following output for the "Blue flower" node:

0 - Weblog
0 - Galleries
1 - Misc flowers
2 - Red flower
2 - Blue flower
1 - Landscape
0 - Products

 

To skip the first level, set the "depth_skip" parameter to 1 by changing the first line of the code fragment as shown below:

{def $mainMenu=treemenu( $module_result.path, $module_result.node_id, , 1, 3 ) }

 

If the "Blue flower" node is being viewed, then the following output will be produced:

0 - Misc flowers
1 - Red flower
1 - Blue flower
0 - Landscape

 

Now, let's use another code fragment in order to see which items are selected:

{def $mainMenu=treemenu( $module_result.path, $module_result.node_id, , , 3 )}
{foreach $mainMenu as $menu}
    {if $menu.is_selected}
        {$menu.level} - {$menu.text} (selected) <br />
    {else}
        {$menu.level} - {$menu.text} <br />
    {/if}
{/foreach}
{undef $mainMenu}

 

Since the "is_selected_method" parameter is omitted, the "tree" mode will be used and the following output will be produced for the "Blue flower" node:

0 - Weblog
0 - Galleries (selected)
1 - Misc flowers (selected)
2 - Red flower
2 - Blue flower (selected)
1 - Landscape
0 - Products

 

To set the "is_selected_method" parameter to "node", replace the first line of the last code fragment by the following line:

{def $mainMenu=treemenu( $module_result.path, $module_result.node_id, , , 3, 'node' )}

 

If the "Blue flower" node is being viewed, then the following output will be produced:

0 - Weblog
0 - Galleries
1 - Misc flowers
2 - Red flower
2 - Blue flower (selected)
1 - Landscape
0 - Products

 

Example 2

These examples are from pagelayout.tpl.

Make a menu of folder and info_page classes. Skip the first level and maximum go to depth 6.

<ul>
{def $mainMenu=treemenu( $module_result.path,
                                    $module_result.node_id,
                                    array('folder','info_page'), 1, 6 )}

{foreach $mainMenu as $menu}
    <li class="level_{$menu.level}">

    {if $menu.is_selected}
        <div class="selected">
        <a href={$menu.url_alias|ezurl}>{$menu.text}</a>
        </div>
    {else}
        <a href={$menu.url_alias|ezurl}>{$menu.text}</a>
    {/if}

    </li>
{/foreach}
</ul>

Make a menu which shows the sub menu items when clicked on the parent menu item. Notice that only the objectclass ids: 1, 9, and 17 are visible.

{def $docs=treemenu( $module_result.path, $module_result.node_id,
                                     array(1, 9, 17), 0, 4)}
{def $depth=1 $last=0}

{foreach $docs as $menu}
    {if and($last | ne(0), $last.level|gt($menu.level))}
        </ul>
        </li>
    {/if}

    <li>

    {if and($last | ne(0), $last.level| lt($menu.level))}
        <ul>
        <li>
    {/if}

    <a {$menu.is_selected|choose('','class="selected"')}
        href={$menu.url_alias|ezurl}>{$menu.text|shorten(25)}</a>
    </li>

    {set last=$menu}
{/foreach}

{while $depth |gt(1)}
    </li>
    </ul>
    {set depth=$depth|sub(1)}
{/while}

Contributors

Comments (2 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. node_id is no longer used

    Svitlana Shatokhina ·

    The "node_id" parameter is no longer used starting from 3.6.11 (rev. 16638), 3.7.9 (rev. 16637), 3.8.4 (rev. 16636), 3.9.0alpha1 (rev. 16635). This parameter has become optional in 3.6.12 (rev.17122), 3.7.10 (rev.17121), 3.8.5 (rev.17120) and deprecated in 3.9.0alpha1 (rev.17119).
    The value specified in the "node_id" parameter is currently ignored by the system. The root of the subtree is now determined by the "path" parameter. If the provided path array points to a node, then a subtree of this node will be fetched. (Use "$module_result.path" to pass the path which leads to the page that is currently being viewed.) If the "path" parameter does not point to a node in the content tree, then a subtree of the "Content" top level node (node ID=2) will be fetched.

Comments

Add a comment

Comments are read by a moderator before they appear.