Bitrix Site Manager

CMain::GetMenuHtmlEx

string
CMain::GetMenuHtmlEx(
 string type = "left",
 bool use_ext = false,
 mixed template_path = false,
 mixed init_dir = false
)

The method GetMenuHtmlEx emits the HTML code which displays the menu of the specified type. Unlike the method CMain::GetMenuHtml, the menu template is applied only once.

Note
The menu template used by this method must have the variable $sMenu initialised. This variable must contain the HTML code describing the whole menu.

Parameters

ParameterDescription
type Type of the menu.
Optional; left by default.
use_ext If true, the menu is built using files .menu type.menu_ext.php in addition to the standard files .menu type.menu.php. The _ext files allows you to modify the menu array $aMenuLinks as desired, for example add menu items.
Optional; false by default.
template_path Path relative to the site root to the menu template.
Optional; false by default, which specifies to search for the template using the algorithm described in the "Menu" section.
init_dir Folder for which the menu is to be built.
Optional; false by default, which means the current folder.

See Also

Example

The following is an example of the drop-down top menu.

Displaying the menu in the file /bitrix/templates/demo/header.php:

<script language="JavaScript1.2" src="/bitrix/templates/demo/js/ddnmenu.js"></script>
<?echo $APPLICATION->GetMenuHtmlEx("top");?>

File /.top.menu.php:

<?
// main menu
// this file can be edited in the menu editor
$aMenuLinks = Array(
    Array(
        "Main", 
        "/index.php", 
        Array(), 
        Array(), 
        "" 
    ),
    Array(
        "Catalog", 
        "/catalog/", 
        Array(), 
        Array(), 
        "" 
    ),
    Array(
        "Support", 
        "/support/", 
        Array(), 
        Array(), 
        "" 
    ),
    Array(
        "Partners", 
        "/partners/", 
        Array(), 
        Array(), 
        "" 
    ),
    Array(
        "Company", 
        "/about/", 
        Array(), 
        Array(), 
        "" 
    )
);
?>

File /bitrix/templates/demo/top.menu_template.php:

<?
// menu template
$sMenu = '<table width="100%" border="0" 
                 cellspacing="0" cellpadding="0"><tr><td width="0%"><img 
                 src="/bitrix/templates/demo/images/top_menu_corner.gif" 
                 alt="" width="15" height="19"></td>';

for($i=0; $i<count($arMENU); $i++)
{
    $MENU_ITEM = $arMENU[$i];
    extract($MENU_ITEM);

    if($SELECTED)
        $clrtext = 'topmenuact';
    else
        $clrtext = 'topmenu';

    $sMenu .= '<td>';
    $sMenu .= '<table border="0" cellspacing="0" cellpadding="0"><tr>';
    $sMenu .= '<td bgcolor="#7B9DBB" onmouseover="show('.$i.')" 
                   onmouseout="hidden('.$i.')"><a href="'.$link.'" 
                   class="'.$clrtext.'"><nobr> '.$TEXT.' </nobr></a></td>';
    $sMenu .= '<td width="0%" bgcolor="#7B9DBB">';

    if($i<count($arMENU)-1) // add vertical divider after all items 
                            // but not after the last one
        $sMenu .= '<img src="/bitrix/templates/demo/images/top_menu_divider.gif" 
                        width="15" height="19" alt="">';
    else
        $sMenu .= '<img src="/bitrix/templates/demo/images/1.gif" 
                        width="1" height="19" alt="">';

    $sMenu .= '</td></tr></table>';

    // check if menu of left type exists in the folder 
    // that the menu item points to
    $popup_menu = new CMenu("left");
    $popup_menu->Init($LINK);
    if(count($popup_menu->arMenu) > 0)
    {
        // if left menu exists then we display it in the hidden layer
        $popup_menu->template = "/bitrix/templates/demo/popup.menu_template.php";
        $sMenu .= '<div style="position:relative; width: 100%;">';
        $sMenu .= '<div onMouseOver="show('.$i.')" onMouseOut="hidden('.$i.')" 
                        id="menu'.$i.'" 
                        style="visibility: hidden; position: absolute; 
                               z-index: +1; top: 0px;" >';
        $sMenu .= $popup_menu->GetMenuHtmlEx();
        $sMenu .= '</div></div>';
    }
    $sMenu .= '</td>';
}
$sMenu .= '<td width="100%" bgcolor="#7B9DBB"><img 
               src="/bitrix/templates/demo/images/1.gif" 
               width="50" height="1" alt=""></td></tr></table>';
?>

File /bitrix/templates/demo/popup.menu_template.php:

<?
$sMenu =
    '<table border="0" cellspacing="0" cellpadding="0" width="110">'.
    '<tr><td bgcolor="#E6EFF7" valign="top"><table 
             border="0" cellspacing="0" cellpadding="0" width="100%">';

for($i=0; $i<count($arMENU); $i++)
{
    $MENU_ITEM = $arMENU[$i];
    extract($MENU_ITEM);

    if ($PERMISSION > "D")
    {
        $sMenu .=
        '<tr valign="top"><td nowrap onmouseover="this.className=\'popupmenuact\'" 
             onmouseout="this.className=\'popupmenu\'" 
             onclick="window.location=\''.$LINK.'\'"  
             class="popupmenu" style="cursor: hand">'.
        '<nobr><a href="'.$link.'" 
                  style="text-decoration: none;"><font 
                  class="popupmenutext">'.$TEXT.'</font></a>'.
        '</nobr></td></tr>';
    }
    else
    {
        $sMenu .=
        '<tr valign="top"><td nowrap onmouseover="this.className=\'popupmenuact\'" 
             onmouseout="this.className=\'popupmenu\'" 
             onclick="window.location=\''.$LINK.'\'"  
             class="popupmenu" style="cursor: hand">'.
        '<nobr><font class="popupmenuclosed">'.$TEXT.'</font>'.
        '</nobr></td></tr>';
    }
}
$sMenu .= '</table></td></tr></table>';
?>

File /bitrix/templates/demo/js/ddnmenu.js:

var brname=navigator.appName, BrVer='';
if(brname.substring(0,2)=="Mi")
    BrVer='E';
var timer = 0;
lastid = -1;

function show(id)
{
    if (!((document.all) ? 
           document.all['menu'+id] : 
           document.getElementById('menu'+id)))
    {
        return;
    }

    clearTimeout(timer);
    if ((id != lastid) && (lastid!=-1))
        ((document.all) ? 
          document.all['menu'+lastid] : 
          document.getElementById('menu'+lastid)).style.visibility = 'hidden';
    hideElement("SELECT", document.getElementById('menu'+lastid));
    lastid = id;
    ((document.all) ? 
      document.all['menu'+lastid] : 
      document.getElementById('menu'+lastid)).style.visibility = 'visible';
}

function hidden(id)
{
    if (!((document.all) ? 
          document.all['menu'+id] : 
          document.getElementById('menu'+id)))
        return;
    showElement("SELECT");
    timer = setTimeout("if('"+id+"' == '"+lastid+"'){((document.all)?document.all['menu"+lastid+"']:document.getElementById('menu"+lastid+"')).style.visibility = 'hidden';}", 
                       500)
}


function GetPos(el)
{
    if (!el || !el.offsetParent)return false;
    var res=Array()
    res["left"] = el.offsetLeft;
    res["top"] = el.offsetTop;
    var objParent = el.offsetParent;
    while (objParent.tagName.toUpperCase()!="BODY")
    {
        res["left"] += objParent.offsetLeft;
        res["top"] += objParent.offsetTop;
        objParent = objParent.offsetParent;
    }
    res["right"]=res["left"]+el.offsetWidth;
    res["bottom"]=res["top"]+el.offsetHeight;
    return res;
}

function hideElement(elName, Menu)
{
    if(BrVer!='E') return;
    for (i = 0; i < document.all.tags(elName).length; i++)
    {
        Obj = document.all.tags(elName)[i];
        if(!(pMenu=GetPos(Menu)))continue;
        if(!(pObj=GetPos(Obj)))continue;

        if (pObj["left"]  < pMenu["right"] && 
            pMenu["left"] < pObj["right"] && 
            pObj["top"]   < pMenu["bottom"] && 
            pMenu["top"]  < pObj["bottom"])
            Obj.style.visibility = "hidden";
    }
}

function showElement(elName)
{
    if(BrVer!='E') return;
    for (i = 0; i < document.all.tags(elName).length; i++)
    {
        obj = document.all.tags(elName)[i];
        if (!obj || !obj.offsetParent)continue;
        if(obj.style.visibility=="hidden")
            obj.style.visibility = "visible";
    }
}