tpl: Add "param" shortcode
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 27 Nov 2018 15:53:11 +0000 (16:53 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 27 Nov 2018 16:34:03 +0000 (17:34 +0100)
Fixes #4010

docs/content/en/content-management/shortcodes.md
tpl/tplimpl/embedded/templates.autogen.go
tpl/tplimpl/embedded/templates/shortcodes/param.html [new file with mode: 0644]

index 35305764c2275f2854a616b5946b0061901047d8..5bb404fb22b6d21e64627bcf8026a18ed46703a5 100644 (file)
@@ -15,6 +15,7 @@ categories: [content management]
 keywords: [markdown,content,shortcodes]
 draft: false
 aliases: [/extras/shortcodes/]
+testparam: "Hugo Rocks!"
 toc: true
 ---
 
@@ -240,6 +241,24 @@ Using the preceding `instagram` with `hidecaption` example above, the following
 {{< instagram BWNjjyYFxVx hidecaption >}}
 
 
+### `param`
+
+Gets a value from the current `Page's` params set in front matter, with a fall back to the site param value. If will log an `ERROR` if the param with the given key could not be found in either.
+
+```bash
+{{</* param testparam */>}}
+```
+
+Since `testparam` is a param defined in front matter of this page wi the value `Hugo Rocks!`, the above will print:
+
+{{< param testparam >}}
+
+To access deeply nested params, use "dot syntax", e.g:
+
+```bash
+{{</* param "my.nested.param" */>}}
+```
+
 ### `ref` and `relref`
 
 These shortcodes will look up the pages by their relative path (e.g., `blog/post.md`) or their logical name (`post.md`) and return the permalink (`ref`) or relative permalink (`relref`) for the found page.
index defc0f313fe7bbf1db3bed735fe4b46107dfc2e4..76a15e452f8f33627e3e2aeefc135503e4581abb 100644 (file)
@@ -398,6 +398,10 @@ if (!doNotTrack) {
 </style>
 {{ end }}
 {{ end }}`},
+       {`shortcodes/param.html`, `{{- $name := (.Get 0) -}}
+{{- with $name -}}
+{{- with ($.Page.Param .) }}{{ . }}{{ else }}{{ errorf "Param %q not found: %s" $name $.Position }}{{ end -}}
+{{- else }}{{ errorf "Missing param key: %s" $.Position }}{{ end -}}`},
        {`shortcodes/ref.html`, `{{ ref . .Params }}`},
        {`shortcodes/relref.html`, `{{ relref . .Params }}`},
        {`shortcodes/twitter.html`, `{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
diff --git a/tpl/tplimpl/embedded/templates/shortcodes/param.html b/tpl/tplimpl/embedded/templates/shortcodes/param.html
new file mode 100644 (file)
index 0000000..74aa3ee
--- /dev/null
@@ -0,0 +1,4 @@
+{{- $name := (.Get 0) -}}
+{{- with $name -}}
+{{- with ($.Page.Param .) }}{{ . }}{{ else }}{{ errorf "Param %q not found: %s" $name $.Position }}{{ end -}}
+{{- else }}{{ errorf "Missing param key: %s" $.Position }}{{ end -}}
\ No newline at end of file