在(自动导航)模板 Concrete5 中显示自定义属性
Posted
技术标签:
【中文标题】在(自动导航)模板 Concrete5 中显示自定义属性【英文标题】:Displaying custom attribute in (autonav) template Concrete5 【发布时间】:2020-01-05 15:03:54 【问题描述】:我想在导航中显示 2 个自定义属性。所以我创建了属性nav_item_class
和beschrijving
。
属性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
我想要什么:
(主页图标)-主页,主页描述
(信息图标)-信息,信息描述
(联系人图标)- 信息、联系人描述
等等
发生了什么:
(主页图标) - 主页、联系人描述
(信息图标)- 信息、联系人描述
(联系人图标)- 联系人、联系人描述
【问题讨论】:
【参考方案1】:您在这里所做的是,在循环导航项时分配一个变量。然后你走得更远,再次循环 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)
【讨论】:
【参考方案2】:循环不正确。尝试以下代码,这应该可以根据您的需要工作。
<?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
【讨论】:
以上是关于在(自动导航)模板 Concrete5 中显示自定义属性的主要内容,如果未能解决你的问题,请参考以下文章