在(自动导航)模板 Concrete5 中显示自定义属性

Displaying custom attribute in (autonav) template Concrete5

我想在导航中显示 2 个自定义属性。所以我创建了属性 nav_item_classbeschrijving

属性 nav_item_class 显示页面图标并正常工作。 beschrijving 属性(描述)在所有导航项上都相同。它应该显示不同页面的不同属性,而不是在所有导航项中添加的最后一个属性。


<?php View::getInstance()->requireAsset('javascript', 'jquery');

$navItems = $controller->getNavItems();

foreach ($navItems as $ni) {
    $classes = array();


    if ($ni->isCurrent) {
        //class for the page currently being viewed
        $classes[] = 'nav-selected';
    }

    if ($ni->inPath) {
        //class for parent items of the page currently being viewed
        $classes[] = 'nav-path-selected';
    }


    if ($ni->hasSubmenu) {
        //class for items that have dropdown sub-menus
        $classes[] = 'dropdown';
    }

    if (!empty($ni->attrClass)) {
        //class that can be set by end-user via the 'nav_item_class' custom page attribute
        $classes[] = $ni->attrClass;
    }

    if ($ni->cObj->getAttribute('beschrijving')) {
        //custom beschrijving
        $beschrijving = $ni->cObj->getAttribute('beschrijving');
    }

    //Put all classes together into one space-separated string
    $ni->classes = implode(" ", $classes);
}

//*** Step 2 of 2: Output menu HTML ***/

echo '<ul class="nav navbar-nav navbar-right">'; //opens the top-level menu

foreach ($navItems as $ni) {

    echo '<li class="' . $ni->classes . '">'; //opens a nav item

    if ($ni->isEnabled) {
        $ni->hasSubmenu;
    }

    if ($ni->hasSubmenu) {
        echo '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' . $ni->name . '</a>';
    } else {
        echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '"><span class="navwrap"><span class="navimg"><i class="material-icons">' . $ni->attrClass . '</i></span><span class="navtit">' . $ni->name . '</span><span class="navtxt">' . $beschrijving . '</span></span></a>';
    }

    if ($ni->hasSubmenu) {
        echo '<ul class="dropdown-menu">'; //opens a dropdown sub-menu
    } else {
        echo '</li>'; //closes a nav item
        echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
    }
}

echo '</ul>'; //closes the top-level menu

我想要的:

(主页图标)- 主页,主页描述

(信息图标)- 信息,信息描述

(联系人图标)- 信息、联系人说明

等等

发生了什么:

(主页图标)- 主页、联系人描述

(信息图标)- 信息、联系人说明

(联系人图标)- 联系人,联系人说明

您在这里所做的是,在循环导航项时分配一个变量。然后你走得更远,再次循环 navItems。在此循环中,变量具有最后一个 navItem 的值。 您需要做的是,在第一个循环中将属性值分配给 navItem。 navItem 是一个简单的 php StdClass 对象,可以分配新变量,您可以稍后检索。

第一个循环中的内容如下:

foreach ($navItems as $ni) {
    $classes = array();


    if ($ni->isCurrent) {
        //class for the page currently being viewed
        $classes[] = 'nav-selected';
    }

    if ($ni->inPath) {
        //class for parent items of the page currently being viewed
        $classes[] = 'nav-path-selected';
    }


    if ($ni->hasSubmenu) {
        //class for items that have dropdown sub-menus
        $classes[] = 'dropdown';
    }

    if (!empty($ni->attrClass)) {
        //class that can be set by end-user via the 'nav_item_class' custom page attribute
        $classes[] = $ni->attrClass;
    }

    if ($ni->cObj->getAttribute('beschrijving')) {
        //custom beschrijving
        $ni->beschrijving = $ni->cObj->getAttribute('beschrijving');
    } else {
        $ni->beschrijving = '';
    }

    //Put all classes together into one space-separated string
    $ni->classes = implode(" ", $classes);
}

然后在第二个循环中:

foreach ($navItems as $ni) {

    echo '<li class="' . $ni->classes . '">'; //opens a nav item

    if ($ni->isEnabled) {
        $ni->hasSubmenu;
    }

    if ($ni->hasSubmenu) {
        echo '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' . $ni->name . '</a>';
    } else {
        echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '"><span class="navwrap"><span class="navimg"><i class="material-icons">' . $ni->attrClass . '</i></span><span class="navtit">' . $ni->name . '</span><span class="navtxt">' . $ni->beschrijving . '</span></span></a>';
    }

    if ($ni->hasSubmenu) {
        echo '<ul class="dropdown-menu">'; //opens a dropdown sub-menu
    } else {
        echo '</li>'; //closes a nav item
        echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
    }
}

循环不正确。试试下面的代码,这应该可以满足您的需要。

<?php View::getInstance()->requireAsset('javascript', 'jquery');

$navItems = $controller->getNavItems();

foreach ($navItems as $ni) {
    $classes = array();


    if ($ni->isCurrent) {
        //class for the page currently being viewed
        $classes[] = 'nav-selected';
    }

    if ($ni->inPath) {
        //class for parent items of the page currently being viewed
        $classes[] = 'nav-path-selected';
    }


    if ($ni->hasSubmenu) {
        //class for items that have dropdown sub-menus
        $classes[] = 'dropdown';
    }

    if (!empty($ni->attrClass)) {
        //class that can be set by end-user via the 'nav_item_class' custom page attribute
        $classes[] = $ni->attrClass;
    }

    //Put all classes together into one space-separated string
    $ni->classes = implode(" ", $classes);
}

//*** Step 2 of 2: Output menu HTML ***/

echo '<ul class="nav navbar-nav navbar-right">'; //opens the top-level menu

foreach ($navItems as $ni) {

    echo '<li class="' . $ni->classes . '">'; //opens a nav item

    if ($ni->isEnabled) {
        $ni->hasSubmenu;
    }

    if ($ni->cObj->getAttribute('beschrijving')) {
        //custom beschrijving
        $beschrijving = $ni->cObj->getAttribute('beschrijving');
    }

    if ($ni->hasSubmenu) {
        echo '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' . $ni->name . '</a>';
    } else {
        echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '"><span class="navwrap"><span class="navimg"><i class="material-icons">' . $ni->attrClass . '</i></span><span class="navtit">' . $ni->name . '</span><span class="navtxt">' . $beschrijving . '</span></span></a>';
    }

    if ($ni->hasSubmenu) {
        echo '<ul class="dropdown-menu">'; //opens a dropdown sub-menu
    } else {
        echo '</li>'; //closes a nav item
        echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
    }
}

echo '</ul>'; //closes the top-level menu