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