我刚开始使用Umbraco(和razor / mvc)。到目前为止,我已经设法设置了基础设计,但下一步是导航。
我一直在阅读文档并在线查找代码片段,但不幸的是,它们都没有真正帮助(或者我可能无法正确理解)。
我现在使用的代码如下所示:
<ul>
@{
var homeNode = CurrentPage.AncestorsOrSelf(1).First();
// The menu items we want are all of the children of the homepage
// We also check if "Hide in navigation" is on in the menu, we shouldn't show hidden items
var menuItems = homeNode.Children.Where("UmbracoNaviHide == false");
}
<li><a href="@homeNode.Url">@homeNode.Name</a></li>
@foreach (var page in menuItems)
{
if(page.Name!="Banner Settings")
{
<li>
<a href="@page.Url">@page.Name</a>
<!-- If the page has child nodes (2nd level) that are visible and docTypeAlias is Textpage (textpages) -->
@if (page.Textpages.Where("UmbracoNaviHide == false").Count() > 0)
{
<ul>
@foreach (var childPage in page.Children.Where("UmbracoNaviHide == false"))
{
<li><a href="@childPage.Url">@childPage.Name</a></li>
}
</ul>
}
</li>
}
}
</ul>
取自How to create Top navigation Sub Menu in umbraco CMS using partial view.
但不幸的是,正如代码本身所指出的那样,它只显示我正在使用的当前页面并且它是孩子们。
有没有办法显示所有菜单项,包括他们的孩子而不是当前的孩子?我似乎无法弄明白。
答案 0 :(得分:1)
您将在Umbraco中找到如何实现导航的示例模板实现。以下是Umbraco v7.4.3的一个例子
$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
return ( strpos($a, $search_text) !== false );
});
Array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php)
$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
return ( strpos($a, $search_text) !== false );
});
这将为您提供顶级菜单,但您想要的是以递归方式呈现所有孩子,因此Sitemap示例将让您更好地了解如何实现此目标。以下示例再次取自Umbraco v7.4.3。
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@*
This snippet displays a list of links of the pages immediately under the top-most page in the content tree.
This is the home page for a standard website.
It also highlights the current active page/section in the navigation with the css class "current".
*@
@{ var selection = CurrentPage.Site().Children.Where("Visible"); }
<ul>
@foreach (var item in selection)
{
<li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
模板在空白安装中。您无需安装入门工具包即可访问它们。在开发人员部分中,转到“部分视图宏”并单击“创建”,然后您将看到模板选项。