隐藏导航 header 如果它是空的

Hide navigation header if its empty

我想隐藏侧边导航的 header 和页脚(如果它不包含任何值)。

@inherits UmbracoTemplatePage

<div id="LeftMenuHolder">               @*<----Not show/include these if list is empty*@
    <span class="Head-Round-Red">       @*<----Not show/include these if list is empty*@
        <span>@CurrentPage.Name</span>  @*<----Not show/include these if list is empty*@
    </span>@*<----Not show these if list is empty*@

    <ul>
        @if (CurrentPage.Parent.DocumentTypeAlias == "Home")
        {
            foreach (var item in CurrentPage.Children.Where("Visible"))
            {
                <li>
                    <a href="@item.Url">
                        <span>@item.Name</span>
                    </a>
                </li>
            }
        }
        else
        {
            foreach (var sibling in CurrentPage.Parent.Children)
            {
                <li>
                    <a href="@sibling.Url">
                        <span>@sibling.Name</span>
                    </a>
                </li>
            }
        }
    </ul>
    <span class="BoxBot">      @*<----Not show/include these if list is empty*@
        <span></span>          @*<----Not show/include these if list is empty*@
    </span>                    @*<----Not show/include these if list is empty*@
    </div>

我确定这可以用 if-statement 解决,但我不太确定。

您可以像这样检索兄弟节点,然后检查列表是否为空:

IPublishedContent parentNode = Umbraco.AssignedContentItem.Parent;

IEnumerable<IPublishedContent> siblingNodes = Umbraco.
                                              AssignedContentItem.
                                              Parent.
                                              Children.
                                              Where(x => !x.Name.Equals(Umbraco.AssignedContentItem.Name) &&
                                                         ((bool)x.GetProperty("UmbracoNaviHide") == false);

对于您的实施,请使用初始化属性尝试类似的操作(如果您在页面中没有 UmbracoNaviHide,请将其从 Linq 语句中删除),如果您也删除此条件想要显示当前页面项目:Where(x => !x.Name.Equals(Umbraco.AssignedContentItem.Name)

@inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>

@{
     IPublishedContent parentNode = Umbraco.AssignedContentItem.Parent;

     IEnumerable<IPublishedContent> siblingNodes = Umbraco.
                                                   AssignedContentItem.
                                                   Parent.
                                                   Children.
                                                   Where(x => !x.Name.Equals(Umbraco.AssignedContentItem.Name) &&
                                                              ((bool)x.GetProperty("UmbracoNaviHide") == false);
}

@if(siblingNodes.Any())
{
<div id="LeftMenuHolder">               @*<----Not show/include these if list is empty*@
    <span class="Head-Round-Red">       @*<----Not show/include these if list is empty*@
        <span>@CurrentPage.Name</span>  @*<----Not show/include these if list is empty*@
    </span>                             @*<----Not show these if list is empty*@
}

    <ul>
        @if (CurrentPage.Parent.DocumentTypeAlias == "Home")
        {
            foreach (var item in CurrentPage.Children.Where("Visible"))
            {
                <li>
                    <a href="@item.Url">
                        <span>@item.Name</span>
                    </a>
                </li>
            }
        }
        else
        {
            foreach (var sibling in CurrentPage.Parent.Children)
            {
                <li>
                    <a href="@sibling.Url">
                        <span>@sibling.Name</span>
                    </a>
                </li>
            }
        }
    </ul>
@if(siblingNodes.Any())
{
    <span class="BoxBot">      @*<----Not show/include these if list is empty*@
        <span></span>          @*<----Not show/include these if list is empty*@
    </span>                    @*<----Not show/include these if list is empty*@
}
    </div>

此外,Umbraco 变量可从 UmbracoViewPage 获得,因此您必须将继承模型更改为如下所示:@inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>.