HUGO
Menu
GitHub 87548 stars Mastodon

Parent

返回給定菜單項的 parent 屬性。

Syntax

MENUENTRY.Parent

Returns

string

使用以下菜單定義:

menus:
  main:
  - name: Products
    pageRef: /product
    weight: 10
  - name: Product 1
    pageRef: /products/product-1
    parent: Products
    weight: 1
  - name: Product 2
    pageRef: /products/product-2
    parent: Products
    weight: 2
[menus]
  [[menus.main]]
    name = 'Products'
    pageRef = '/product'
    weight = 10
  [[menus.main]]
    name = 'Product 1'
    pageRef = '/products/product-1'
    parent = 'Products'
    weight = 1
  [[menus.main]]
    name = 'Product 2'
    pageRef = '/products/product-2'
    parent = 'Products'
    weight = 2
{
   "menus": {
      "main": [
         {
            "name": "Products",
            "pageRef": "/product",
            "weight": 10
         },
         {
            "name": "Product 1",
            "pageRef": "/products/product-1",
            "parent": "Products",
            "weight": 1
         },
         {
            "name": "Product 2",
            "pageRef": "/products/product-2",
            "parent": "Products",
            "weight": 2
         }
      ]
   }
}

此模板渲染嵌套菜單,在每個子條目旁邊列出 parent 屬性:

<ul>
  {{ range .Site.Menus.main }}
    <li>
      <a href="{{ .URL }}">{{ .Name }}</a>
      {{ if .HasChildren }}
        <ul>
          {{ range .Children }}
            <li><a href="{{ .URL }}">{{ .Name }}</a> ({{ .Parent }})</li>
          {{ end }}
        </ul>
      {{ end }}
    </li>
  {{ end }}
</ul>

Last updated: January 1, 0001
Improve this page