aboutsummaryrefslogtreecommitdiffhomepage
path: root/content/en/methods/shortcode/Parent.md
blob: c500af3759d1a164d1ddeb3c46a5393da6d4995e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
---
title: Parent
description:  Returns the parent shortcode context in nested shortcodes.
categories: []
keywords: []
action:
  related: []
  returnType: hugolib.ShortcodeWithPage
  signatures: [SHORTCODE.Parent]
---

This is useful for inheritance of common shortcode arguments from the root.

In this contrived example, the "greeting" shortcode is the parent, and the "now" shortcode is child.

{{< code file=content/welcome.md lang=md >}}
{{</* greeting dateFormat="Jan 2, 2006" */>}}
Welcome. Today is {{</* now */>}}.
{{</* /greeting */>}}
{{< /code >}}

{{< code file=layouts/shortcodes/greeting.html  >}}
<div class="greeting">
  {{ trim .Inner "\r\n" | .Page.RenderString }}
</div>
{{< /code >}}

{{< code file=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 -}}
{{< /code >}}

The "now" shortcode formats the current time using:

1. The `dateFormat` argument passed to the "now" shortcode, if present
2. The `dateFormat` argument passed to the "greeting" shortcode, if present
3. The default layout string defined at the top of the shortcode