【问题标题】:Umbraco 7.4.3 dropdown navigationUmbraco 7.4.3 下拉导航
【发布时间】:2016-09-01 08:03:11
【问题描述】:

我刚开始使用 Umbraco(和 razor/mvc)。到目前为止,我已经成功设置了基本设计,但下一步是导航。

我一直在阅读文档并在线寻找代码 sn-ps,但不幸的是,它们都没有真正的帮助(或者我可能无法正确理解)。

我现在使用的代码如下所示:

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

但不幸的是,正如代码本身所指出的那样,它只显示我所在的当前页面及其子页面。

有没有办法显示所有菜单项,包括他们的孩子而不是当前的?我好像搞不懂。

【问题讨论】:

    标签: razor model-view-controller umbraco


    【解决方案1】:

    您实际上会在 Umbraco 中找到有关如何实现导航的示例模板实现。下面是直接来自 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>
    

    这将为您提供顶级菜单,但您想要的是递归地渲染所有子项,因此站点地图示例将使您更好地了解如何实现这一点。以下示例再次取自 Umbraco v7.4.3。

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @*
        This snippet makes a list of links of all visible pages of the site, as nested unordered html lists.
    
        How it works:
        - It uses a custom Razor helper called Traverse() to select and display the markup and links.
    *@
    
    @{ var selection = CurrentPage.Site(); }
    
    <div class="sitemap">
        @* Render the sitemap by passing the root node to the traverse helper, below *@
        @Traverse(selection)
    </div>
    
    
    @* Helper method to travers through all descendants *@
    @helper Traverse(dynamic node)
    {
        @* Update the level to reflect how deep you want the sitemap to go *@
        var maxLevelForSitemap = 4;
    
        @* Select visible children *@
        var selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
        @* If any items are returned, render a list *@
        if (selection.Any())
        {
            <ul>
                @foreach (var item in selection)
                {
                    <li class="level-@item.Level">
                        <a href="@item.Url">@item.Name</a>
    
                        @* Run the traverse helper again for any child pages *@
                        @Traverse(item)
                    </li>
                }
            </ul>
        }
    }
    

    模板在空白安装中。您无需安装入门工具包即可访问它们。在开发人员部分转到部分视图宏并单击创建,然后您将看到模板选项。

    【讨论】:

    • 我为延误道歉!但是非常感谢这些sn-ps!我在没有模板的情况下安装了 umbraco,所以我不知道这些。谢谢!这对我继续前进有很大帮助。
    • 模板在空白安装中。您无需安装入门工具包即可访问它们。在开发者部分转到部分视图宏并单击创建,然后您将看到模板选项。
    • 我不知道那个 ProNotion,谢谢你的提示。但是如果我插入上面的代码(站点地图代码),它仍然只显示子页面。
    • 你的内容结构是什么样的?能举个例子吗?
    猜你喜欢
    • 2015-09-08
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    相关资源
    最近更新 更多