父级
语法
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”短代码使用以下方式格式化当前时间
- 传递给“now”短代码的
dateFormat
参数(如果存在) - 传递给“greeting”短代码的
dateFormat
参数(如果存在) - 短代码顶部定义的默认布局字符串