订购菜单项

时间:2016-02-14 15:04:05

标签: hugo

我正在尝试在菜单中订购商品,但这对我不起作用。我试图按照他们的文档进行跟踪,但它根本不起作用。

在我的标题中:

      {{ range .Site.Data.Menu }}
      <li>

        <a href="{{ .URL | absURL }}"
        {{ if and ( isset . "Title" ) ( ne .Title "" ) }} title="{{ .Title }}"{{ end }}>

        {{ if and ( isset . "IconClass" ) ( ne .IconClass "" )  }}
            <i class="fa {{ .IconClass }}"></i>
        {{ end }}

        {{ .Name }}
        </a>
      </li>
      {{ end }}

menu.toml

[home]
    Name = "Home"
    Title = "Home"
    URL = "/home"
    weight = 1

[apparatus]
    Name = "Apparatus"
    URL = "/apparatus"
    weight = 2

[deliveries]
    Name = "Deliveries"
    URL = "/deliveries"
    weight = 3

[command]
    Name = "Command"
    URL = "/command"
    weight = 4

[ambulance]
    Name = "Ambulance"
    URL = "/ambulance"
    weight = 5

[service]
    Name = "Service"
    URL = "/service"
    weight = 6

[about]
    Name = "about"
    URL = "/about"
    weight = 7

[contact]
    Name = "Contact"
    URL = "/contact"
    weight = 8

菜单以看似随机的顺序结束。我如何以我想要的方式订购它们?

3 个答案:

答案 0 :(得分:3)

重量是Hugo中菜单的默认排序。下面是一些变化:

{{ range .Site.Data.Menu.Sort }}
{{ range .Site.Data.Menu.ByName }}
{{ range .Site.Data.Menu.ByName.Reverse }}
{{ range .Site.Data.Menu.ByName.Limit 10 }}
{{ range .Site.Data.Menu.ByWeight }}

答案 1 :(得分:2)

我遇到了同样的问题,并通过使用负数来表示权重属性。

答案 2 :(得分:2)

更改行:

{{ range .Site.Data.Menu }}

由:

{{ range sort .Site.Data.Menu "weight"}}

这解决了我的问题。