HUGO
Menu
GitHub 87548 stars Mastodon

Parent

在嵌套 shortcode 中返回父 shortcode 上下文。

Syntax

SHORTCODE.Parent

Returns

hugolib.ShortcodeWithPage

這對於從根繼承常見 shortcode 參數很有用。

在這個人為示例中,“greeting” shortcode 是父級,“now” shortcode 是子級。

content/welcome.md
{{< greeting dateFormat="Jan 2, 2006" >}}
Welcome. Today is {{< now >}}.
{{< /greeting >}}
layouts/_shortcodes/greeting.html
<div class="greeting">
  {{ .Inner | strings.TrimSpace | .Page.RenderString }}
</div>
layouts/_shortcodes/now.html
{{- $dateFormat := "January 2, 2006 15:04:05" }}

{{- with .Params }}
  {{- with .dateFormat }}
    {{- $dateFormat = . }}
  {{- end }}
{{- else }}
  {{- with .Parent.Params }}
    {{- with .dateFormat }}
      {{- $dateFormat = . }}
    {{- end }}
  {{- end }}
{{- end }}

{{- now | time.Format $dateFormat -}}

“now” shortcode 使用以下格式格式化當前時間:

  1. 傳遞給 “now” shortcode 的 dateFormat 參數(如果存在)
  2. 傳遞給 “greeting” shortcode 的 dateFormat 參數(如果存在)
  3. 在 shortcode 頂部定義的默認布局字符串

Last updated: January 1, 0001
Improve this page