父级
Syntax
SHORTCODE.Parent
Returns
hugolib.ShortcodeWithPage
这对于继承根目录中常见的短代码参数很有用。
在这个人为编造的例子中,“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” 短代码使用以下格式显示当前时间:
- 如果存在,则使用传递给“now”短代码的
dateFormat
参数 - 如果存在,则使用传递给“greeting”短代码的
dateFormat
参数 - 使用短代码顶部定义的默认布局字符串