Documentation

GetMenuHtml

string
CMain::GetMenuHtml(
 string type = "left",
 bool MenuExt = false,
 mixed template = false,
 mixed InitDir = false
)

The method returns the HTML code which displays the menu of the specified type. Unlike the CMain::GetMenuHtmlEx method, the menu template is applied to every item of the menu. Non-static method.

Parameters

ParameterDescription Available from version
type Menu type.
Optional parameter. By default value is left.
MenuExt 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, sourcing them from information blocks.
Optional; false by default.
template 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.
InitDir Folder for which the menu is to be built.
Optional; false by default, which means the current folder.

See Also

The following is an example of the left menu with items collected from the information block.

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

<?
echo $APPLICATION->GetMenuHtml("left", true);
?>

File /.left.menu.php:

<?
// main menu
// this file can be edited in the menu editor
$aMenuLinks = Array(
);
?>

File /.left.menu_ext.php:

<?
// additional menu
// adds sections of the phone catalogue to the main menu array
if (CModule::IncludeModule("iblock")):

    $IBLOCK_TYPE = "catalog";   // information block type
    $IBLOCK_ID = 21;            // information block ID
    $CACHE_TIME = 3600;         // cache for 1 hour

    $aMenuLinksNew = array();

    // create an object to cache the menu
    $CACHE_ID = __FILE__.$IBLOCK_ID;
    $obMenuCache = new CPHPCache;
    // if the array is cached...
    if($obMenuCache->InitCache($CACHE_TIME, $CACHE_ID, "/"))
    {
        // take data from the cache
        $arVars = $obMenuCache->GetVars();
        $aMenuLinksNew = $arVars["aMenuLinksNew"];
    }
    else
    {
        // otherwise, build it
        $rsSections = GetIBlockSectionList($IBLOCK_ID, false, array("SORT" => "ASC", "ID" => "ASC"), false, array("ACTIVE"=>"Y"));
        while ($arSection = $rsSections->Fetch())
        {
            $arrAddLinks = array(SITE_DIR."catalog/phone/element.php?SECTION_ID=".$arSection["ID"]);
            // iterate the section elements
            if ($rsElements = GetIBlockElementListEx($IBLOCK_TYPE, false, false, array(), false, array("ACTIVE" => "Y", "IBLOCK_ID" => $IBLOCK_ID, "SECTION_ID" => $arSection["ID"]), array("ID", "IBLOCK_ID", "DETAIL_PAGE_URL")))
            {
                while ($arElement = $rsElements->GetNext()) $arrAddLinks[] = $arElement["DETAIL_PAGE_URL"];
            }
            $aMenuLinksNew[] = array(
                $arSection["NAME"], 
                SITE_DIR."catalog/phone/section.php?SECTION_ID=".$arSection["ID"],
                $arrAddLinks);        
        }
    }
    // store data in the cache
    if($obMenuCache->StartDataCache())
    {
        $obMenuCache->EndDataCache(Array("aMenuLinksNew" => $aMenuLinksNew));
    }

    // merge the main and additional menu arrays
    $aMenuLinks = array_merge($aMenuLinksNew, $aMenuLinks);

endif;
?>

Menu template /bitrix/templates/demo/left.menu_template.php:

<?
// This file is the template for one menu item iteration

// Set item mark: selected folder, folder, page
if ($ITEM_TYPE=="D")
{
    if ($SELECTED)
        $strDir = "<td width='0%' bgcolor='#A0C4E0' valign='middle' align='center'><img height='13' src='/bitrix/templates/demo/images/left_folder_open.gif' width='17' border='0'></td>";
    else
        $strDir = "<td width='0%' bgcolor='#CCDFEE' valign='middle' align='center'><img height='13' src='/bitrix/templates/demo/images/left_folder.gif' width='17' border='0'></td>";
}
else
{
    if ($SELECTED)
    {
        $strDir = "<td width='0%' bgcolor='#A0C4E0' valign='middle' align='center'><img height='13' src='/bitrix/templates/demo/images/left_bullet.gif' width='17' border='0'></td>";
        $strDir_d = "<td width='0%' bgcolor='#A0C4E0' valign='middle' align='center'><img height='13' src='/bitrix/templates/demo/images/left_bullet_d.gif' width='17' border='0' alt='Private section'></td>";
    }
    else
    {
        $strDir = "<td width='0%' bgcolor='#CCDFEE' valign='middle' align='center'><img height='13' src='/bitrix/templates/demo/images/left_bullet.gif' width='17' border='0'></td>";
        $strDir_d = "<td width='0%' bgcolor='#CCDFEE' valign='middle' align='center'><img height='13' src='/bitrix/templates/demo/images/left_bullet_d.gif' width='17' border='0' alt='Private section'></td>";
    }
}

// if $SELECTED then this item is current (active) item
if ($SELECTED)
    $strtext = "leftmenuact";
else
    $strtext = "leftmenu";
    
//if $PARAMS["SEPARATOR"]=="Y" this item should be shown with different style applied

if ($PARAMS["SEPARATOR"]=="Y")
{
    $strstyle = " style='background-color: #D5ECE6; border-top: 1px solid #A6D0D7; border-bottom: 1px solid #A6D0D7; padding:8;'";
    $strDir = "<img height='13' src='/bitrix/templates/demo/images/1.gif' width='17' border='0'>";
    $strtext = "leftmenu";
}
else
    $strstyle = " style='padding:8;'";


// Content of variable $sMenuProlog is typed just before all menu items iterations
// Content of variable $sMenuEpilog is typed just after all menu items iterations
$sMenuProlog = "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
$sMenuEpilog = '<tr><td colspan=2 background="/bitrix/templates/demo/images/l_menu_border.gif"><img src="/bitrix/templates/demo/images/1.gif" width="1" height="1"></td></tr></table>';

// if $PERMISSION > "D" then current user can access this page
if ($PERMISSION > "D")
{
    $sMenuBody = '<tr><td colspan=2 background="/bitrix/templates/demo/images/l_menu_border.gif"><img src="/bitrix/templates/demo/images/1.gif" width="1" height="1"></td></tr><tr>'.$strDir.'<td valign="top"'.$strstyle.' width="100%"><a href="'.$LINK.'" class="'.$strtext.'">'.$TEXT.'</a></td></tr>';
}
else
{
    $sMenuBody = '<td colspan=2 background="/bitrix/templates/demo/images/l_menu_border.gif"><img src="/bitrix/templates/demo/images/1.gif" width="1" height="1"></td></tr><tr>'.$strDir_d.'</td><td valign="top"'.$strstyle.' width="100%"><a href="'.$LINK.'" class='.$strtext.'>'.$TEXT.'</a></td></tr>';

}
?>


© «Bitrix24», 2001-2024
Up