雨果:如何在部分页面中显示一个子部分列表

问题描述 投票:2回答:1

这将是一个简单的问题,但我已经花了很多时间,我找不到明确的答案。以下是我的博客的当前状态:https://brucou.github.io/

Projects中,我有以下目录结构:

Projects
  Component combinators
  Trees and forests
  Circuitry

在我的项目页面(/projects/)中,我想显示相应子文件夹的列表,即Component combinatorsTrees and forests等,以及相应的永久链接。

我在/layouts/projects/list.html有这个代码

<ul>
  {{ template "section-tree-nav" .Site.Home }}
</ul>
{{ define "section-tree-nav" }}
  {{ range .Sections}}
    <li>{{ .Title }}
        <ul>
            {{ range .Pages }}
            <li><a href="{{ .RelPermalink}}">{{ .Title }}</a></li>
            {{ end }}
            <ul>
                {{ template "section-tree-nav" . }}
            </ul>
        </ul>
    </li>
  {{ end }}
{{ end }}

目前,此代码显示在项目页面中,即目录树,例如:

Posts
  Reactive programming : a component-based approach
  A componentization model for cyclejs
  Componentization against complexity
  User interfaces as reactive systems
Projects
  Component combinators

我的问题是:

  • 如何只显示Projects目录内容?或者基本上如何在标题上做一个if?内容?项目目录?

那就是我只想在项目页面中显示:

  Component combinators
  Trees and forests
hugo
1个回答
3
投票

解决这个问题:

{{ partial "header" . }}

<article>
    <header>
        {{ partial "title" . }}
        {{ with .Content }}{{.}}{{ end }}
    </header>

    {{ if (eq $.Parent.Title "Projects") }}
      <ul class="no-bullet">
          {{ range .Paginator.Pages }}
          {{ partial "li" . }}
          {{ end }}
      </ul>
      {{ partial "paginator" . }}
    {{ else }}
      {{ range (where .Site.Pages "Section" "projects") }}
        <ul class="no-bullet">
            {{ range .Sections }}
            <li>
                <span><time datetime="{{.Date}}">{{ .Date.Format .Site.Params.DateFmt }}</time></span>
                <a href="{{ .Permalink }}">{{ .Title }}</a>
            </li>
            {{ end }}
        </ul>
      {{ end }}
    {{ end }}

</article>
{{ partial "footer" . }}


{{/* keep the / right next to the parenthesis */}}
{{/* printf "%#v" $.CurrentSection.Title */}}
{{/* printf "%#v" (eq $.Parent.Title "Projects") */}}

基本上,我们在层次结构层面上进行分支。如果在顶层,那么我们显示目录(存储在.Sections中),如果没有,我们显示页面。

© www.soinside.com 2019 - 2024. All rights reserved.