Shortcode

Parent

在嵌套短代码中返回父短代码的上下文。

签名
SHORTCODE.Parent
返回类型
hugolib.ShortcodeWithPage

这对于从根级继承公共的短代码参数很有用。

在这个刻意构造的示例中,「greeting」短代码是父级,「now」短代码是子级。

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」短代码按下面的顺序格式化当前时间:

  1. 传给「now」短代码的 dateFormat 参数(如果存在)
  2. 传给「greeting」短代码的 dateFormat 参数(如果存在)
  3. 短代码顶部定义的默认布局字符串