Parent

這對於從根短代碼繼承共同的短代碼參數非常有用。

在這個設計範例中,“greeting” 短代碼是父項,“now” 短代碼是子項。

content/welcome.md
{{< greeting dateFormat="Jan 2, 2006" >}}
歡迎。今天是 {{< 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” 短代碼會根據以下條件格式化當前時間:

  1. 如果 “now” 短代碼傳遞了 dateFormat 參數,則使用該參數
  2. 如果 “greeting” 短代碼傳遞了 dateFormat 參數,則使用該參數
  3. 使用短代碼頂部定義的預設日期格式字符串