HUGO
Menu
GitHub 87548 stars Mastodon

Reverse

返回給定菜單,反轉其條目的排序順序。

Syntax

MENU.Reverse

Returns

navigation.Menu

Reverse 方法返回給定菜單,反轉其條目的排序順序。

考慮以下菜單定義:

menus:
  main:
  - name: Services
    pageRef: /services
    weight: 10
  - name: About
    pageRef: /about
    weight: 20
  - name: Contact
    pageRef: /contact
    weight: 30
[menus]
  [[menus.main]]
    name = 'Services'
    pageRef = '/services'
    weight = 10
  [[menus.main]]
    name = 'About'
    pageRef = '/about'
    weight = 20
  [[menus.main]]
    name = 'Contact'
    pageRef = '/contact'
    weight = 30
{
   "menus": {
      "main": [
         {
            "name": "Services",
            "pageRef": "/services",
            "weight": 10
         },
         {
            "name": "About",
            "pageRef": "/about",
            "weight": 20
         },
         {
            "name": "Contact",
            "pageRef": "/contact",
            "weight": 30
         }
      ]
   }
}

要按名稱降序排序條目:

<ul>
  {{ range .Site.Menus.main.ByName.Reverse }}
    <li><a href="{{ .URL }}">{{ .Name }}</a></li>
  {{ end }}
</ul>

Hugo 將其渲染為:

<ul>
  <li><a href="/services/">Services</a></li>
  <li><a href="/contact">Contact</a></li>
  <li><a href="/about/">About</a></li>
</ul>

Last updated: January 1, 0001
Improve this page