雨果:如何在配置文件中特定于部分的菜单中显示子页面?

发布于 2025-02-02 16:25:40 字数 4143 浏览 5 评论 0原文

我正在使用Hugo建立一个文档网站。它具有主要的NAV和侧栏NAV。研究之后,我使用配置文件降落了菜单。菜单呈现正确,但是我坚持如何根据我使用的配置文件进行渲染侧边栏导航(请参见下面的示例)。

页面布局的屏幕截图

这是我的config json。如您所见,我正在调用一个“主”菜单,但使用“父”进行导航,这将属于侧边栏导航。

{
    "baseURL": "http://example.com/",
    "languageCode": "en-us",
    "title": "DS",
    "menu": {
        "main": [
            {
                "name": "Getting Started",
                "url": "/getting-started",
                "pageRef": "getting-started",
                "weight": 10
            },
            {
                "name": "Contents",
                "url": "/contents",
                "pageRef": "contents",
                "parent": "Getting Started"
            },
            {
                "name": "Download",
                "url": "/download",
                "pageRef": "download",
                "parent": "Getting Started"
            },
            {
                "name": "Foundations",
                "url": "/foundations",
                "pageRef": "foundations",
                "weight": 20
            },
            {
                "name": "Colors",
                "url": "/colors",
                "pageRef": "colors",
                "parent": "Foundations"
            },
            {
                "name": "Grid",
                "url": "/grid",
                "pageRef": "grid",
                "parent": "Foundations"
            },
            {
                "name": "Components",
                "url": "/components",
                "pageRef": "components",
                "weight": 30
            },
            {
                "name": "Alerts",
                "url": "/alerts",
                "pageRef": "alerts",
                "parent": "Components"
            },
            {
                "name": "Badges",
                "url": "/badges",
                "pageRef": "badges",
                "parent": "Components"
            },
            {
                "name": "Utilities",
                "url": "/utilities",
                "pageRef": "utilities",
                "weight": 40
            },
            {
                "name": "Background",
                "url": "/background",
                "pageRef": "background",
                "parent": "Utilities"
            },
            {
                "name": "Borders",
                "url": "/borders",
                "pageRef": "borders",
                "parent": "Utilities"
            }
        ]
    }
}

这是我的主航的代码,它运行良好。

<header class="header">
    <nav class="header__nav">
        <div class="container header__nav-container">
            <div class="header__brand">
                <a href="{{ .Site.BaseURL }}" class="header__brand-type-link">{{ .Site.Title }}</a>
            </div>

            <ul class="header__nav-list">
                {{- $currentPage := . -}} 
                
                {{- range .Site.Menus.main -}}
                <li class="header__nav-item">
                    <a href="{{ .URL }}" class='header__nav-link {{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}current_page_link{{end}}'>{{ .Name }}</a>
                </li>
                {{ end }}
            </ul>
        </div>
    </nav>
</header>

这是我的侧边栏导航的代码。如果我不需要基于您所处的部分显示导航,这将效果很好。我觉得与我的范围内的.Children有一些可靠的东西。我尝试了.site.Section.Children之类的方法,但这无效。关于我在这里缺少的东西有什么想法吗?

<nav class="sidebar__nav">
    <ul class="sidebar__nav-list">
        {{- $currentPage := . -}} 
        
        {{- range .Site.Menus.main -}}
                {{- range .Children -}}
                <li class="sidebar__nav-item">
                    <a href="{{ .URL }}" class="sidebar__nav-link {{ if $currentPage.IsMenuCurrent `main` . }}current_page_link{{end}}">{{ .Name }}</a>
                </li>
                {{ end }}
        {{ end }}
    </ul>
</nav>

I’m building out a documentation site using Hugo. It has a Main nav and Sidebar nav. After researching I’ve landed on building out my menu using the config file. The menus render correctly, but I’m stuck on how to render my sidebar navigation based on the section I’m in using the config file (see example below).

Screen shot of page layout

Here’s my config json. As you can see I’m calling out a single “main” menu, but using “parent” for navigation that would fall under the sidebar nav.

{
    "baseURL": "http://example.com/",
    "languageCode": "en-us",
    "title": "DS",
    "menu": {
        "main": [
            {
                "name": "Getting Started",
                "url": "/getting-started",
                "pageRef": "getting-started",
                "weight": 10
            },
            {
                "name": "Contents",
                "url": "/contents",
                "pageRef": "contents",
                "parent": "Getting Started"
            },
            {
                "name": "Download",
                "url": "/download",
                "pageRef": "download",
                "parent": "Getting Started"
            },
            {
                "name": "Foundations",
                "url": "/foundations",
                "pageRef": "foundations",
                "weight": 20
            },
            {
                "name": "Colors",
                "url": "/colors",
                "pageRef": "colors",
                "parent": "Foundations"
            },
            {
                "name": "Grid",
                "url": "/grid",
                "pageRef": "grid",
                "parent": "Foundations"
            },
            {
                "name": "Components",
                "url": "/components",
                "pageRef": "components",
                "weight": 30
            },
            {
                "name": "Alerts",
                "url": "/alerts",
                "pageRef": "alerts",
                "parent": "Components"
            },
            {
                "name": "Badges",
                "url": "/badges",
                "pageRef": "badges",
                "parent": "Components"
            },
            {
                "name": "Utilities",
                "url": "/utilities",
                "pageRef": "utilities",
                "weight": 40
            },
            {
                "name": "Background",
                "url": "/background",
                "pageRef": "background",
                "parent": "Utilities"
            },
            {
                "name": "Borders",
                "url": "/borders",
                "pageRef": "borders",
                "parent": "Utilities"
            }
        ]
    }
}

Here’s my code for the Main nav which works great.

<header class="header">
    <nav class="header__nav">
        <div class="container header__nav-container">
            <div class="header__brand">
                <a href="{{ .Site.BaseURL }}" class="header__brand-type-link">{{ .Site.Title }}</a>
            </div>

            <ul class="header__nav-list">
                {{- $currentPage := . -}} 
                
                {{- range .Site.Menus.main -}}
                <li class="header__nav-item">
                    <a href="{{ .URL }}" class='header__nav-link {{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}current_page_link{{end}}'>{{ .Name }}</a>
                </li>
                {{ end }}
            </ul>
        </div>
    </nav>
</header>

Here’s the code for my Sidebar nav. This would work great if I didn’t need the navigation to display based on the section you’re in. I have a feeling it has something to to with the .Children in my range. I tried methods like .Site.Section.Children, but that didn’t work. Any thoughts on what I’m missing here?

<nav class="sidebar__nav">
    <ul class="sidebar__nav-list">
        {{- $currentPage := . -}} 
        
        {{- range .Site.Menus.main -}}
                {{- range .Children -}}
                <li class="sidebar__nav-item">
                    <a href="{{ .URL }}" class="sidebar__nav-link {{ if $currentPage.IsMenuCurrent `main` . }}current_page_link{{end}}">{{ .Name }}</a>
                </li>
                {{ end }}
        {{ end }}
    </ul>
</nav>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2025-02-09 16:25:40

我相信您可以使用{{$ proction:= $ currentpage.section}}获得当前页面的部分{ - 如果eq .parent $ pection-}} 。我没有测试过这一点,但理论上应该起作用。您还应该检查菜单条目本身是否有孩子,如果没有孩子,即使是当前页面,也只会将其显示为独立条目。

<ul class="sidebar__nav-list">
  {{- $currentPage := . -}}
  {{- $section := $currentPage.Section -}}
  {{- range .Site.Menus.main -}}
    {{- if .HasChildren -}}
      <li class="sidebar__nav-item">
        <a href="{{ .URL }}" class="sidebar__nav-link {{ if $currentPage.IsMenuCurrent `main` . }}current_page_link{{end}}">
          {{ .Name }}
        </a>
        <ul class="sub-menu">
          {{ range .Children }}
            {{- if eq .Parent $section -}}
              <li class="sidebar__nav-item">
                <a href="{{ .URL }}" class="sidebar__nav-link">{{ .Name }}</a>                  
              </li>
            {{ end }}
          {{ end }}
        </ul>
      </li>
    {{ else }}
      <li class="sidebar__nav-item">
        <a href="{{ .URL }}" class="sidebar__nav-link {{ if $currentPage.IsMenuCurrent `main` . }}current_page_link{{end}}">
          {{ .Name }}              
        </a>
      </li>
    {{ end }}
  {{ end }}
</ul>

I believe you could get the section of the current page with {{ $section := $currentPage.Section }}, then check if the child menu entry's parent matches the current page's section with {{- if eq .Parent $section -}}. I haven't tested this but it should work in theory. You should also probably check if the menu entry itself even has children and if not, only display that as a standalone entry even if it's the current page.

<ul class="sidebar__nav-list">
  {{- $currentPage := . -}}
  {{- $section := $currentPage.Section -}}
  {{- range .Site.Menus.main -}}
    {{- if .HasChildren -}}
      <li class="sidebar__nav-item">
        <a href="{{ .URL }}" class="sidebar__nav-link {{ if $currentPage.IsMenuCurrent `main` . }}current_page_link{{end}}">
          {{ .Name }}
        </a>
        <ul class="sub-menu">
          {{ range .Children }}
            {{- if eq .Parent $section -}}
              <li class="sidebar__nav-item">
                <a href="{{ .URL }}" class="sidebar__nav-link">{{ .Name }}</a>                  
              </li>
            {{ end }}
          {{ end }}
        </ul>
      </li>
    {{ else }}
      <li class="sidebar__nav-item">
        <a href="{{ .URL }}" class="sidebar__nav-link {{ if $currentPage.IsMenuCurrent `main` . }}current_page_link{{end}}">
          {{ .Name }}              
        </a>
      </li>
    {{ end }}
  {{ end }}
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文