From f37c5a25676db89c0e804ccaac69bb392758192b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 27 Nov 2018 16:53:11 +0100 Subject: [PATCH] tpl: Add "param" shortcode Fixes #4010 --- .../en/content-management/shortcodes.md | 19 +++++++++++++++++++ tpl/tplimpl/embedded/templates.autogen.go | 4 ++++ .../embedded/templates/shortcodes/param.html | 4 ++++ 3 files changed, 27 insertions(+) create mode 100644 tpl/tplimpl/embedded/templates/shortcodes/param.html diff --git a/docs/content/en/content-management/shortcodes.md b/docs/content/en/content-management/shortcodes.md index 35305764c..5bb404fb2 100644 --- a/docs/content/en/content-management/shortcodes.md +++ b/docs/content/en/content-management/shortcodes.md @@ -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 +{{}} +``` + +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 +{{}} +``` + ### `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. diff --git a/tpl/tplimpl/embedded/templates.autogen.go b/tpl/tplimpl/embedded/templates.autogen.go index defc0f313..76a15e452 100644 --- a/tpl/tplimpl/embedded/templates.autogen.go +++ b/tpl/tplimpl/embedded/templates.autogen.go @@ -398,6 +398,10 @@ if (!doNotTrack) { {{ 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 index 000000000..74aa3ee7b --- /dev/null +++ b/tpl/tplimpl/embedded/templates/shortcodes/param.html @@ -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