0

I want to display 2 custom attributes in the navigation. So I created the attribute nav_item_class and beschrijving.

The attribute nav_item_class displays the page icon and works correctly. The attribute beschrijving (description) is the same on all navigation items. It should show the different attribute of the different pages instead of the last attribute added at all the navigation items.


<?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

What I want:

(Home icon) - Home, Home description

(Info icon) - Info, Info description

(Contact icon) - Info, Contact Description

etc.

What is happening:

(Home icon) - Home, Contact description

(Info icon) - Info, Contact Description

(Contact icon) - Contact, Contact Description

4

2 回答 2

1

您在这里所做的是,在循环 navItems 时分配一个变量。然后你走得更远,再次循环 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)
    }
}
于 2019-09-03T06:30:45.407 回答
0

循环不正确。尝试以下代码,这应该可以根据您的需要工作。

<?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
于 2019-09-04T10:16:20.380 回答