首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hugo :如何在部分页面中显示一个子部分列表

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

Stack Overflow用户
提问于 2017-12-20 11:16:06
回答 1查看 4.4K关注 0票数 2

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

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

代码语言:javascript
复制
Projects
  Component combinators
  Trees and forests
  Circuitry

在我的项目页面(/projects/)中,我希望用相应的permalinks显示相应子文件夹的列表,即Component combinatorsTrees and forests等。

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

代码语言:javascript
复制
<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 }}

当前,此代码显示在“项目”页中的目录树中,例如:

代码语言:javascript
复制
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?内容?项目目录的吗?

也就是说,我只想在“项目”页面中显示此内容:

代码语言:javascript
复制
  Component combinators
  Trees and forests
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-25 20:59:13

就这样解决了:

代码语言:javascript
复制
{{ 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中),如果没有,则显示页面。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47904764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档