Accessing a frontmatter parameter with Shortcode param fails when the parameter is boolean and has value false
.
---
title: param test
bool1: true
---
{{< param bool1 >}}
gives true
.
---
title: param test
bool1: false
---
{{< param bool1 >}}
rises error Param "bool1" not found: "/content/test/p.md:6:1" logged 1 error(s)
idarek
2
Because when it is false it’s like it’s not there.
If you want to return false than you need to convert that into a string.
This is the only situation where I have found the isset
function to be useful.
You can override the internal “param” shortcode.
mkdir -p layouts/shortcodes
touch layouts/shortcodes/param.html
Then place this in the new file:
{{- $value := "" }}
{{- with $name := .Get 0 }}
{{- if $.Params }}
{{- if isset $.Page.Params $name }}
{{- if in (slice true "true") ($.Page.Param $name) }}
{{- $value = true }}
{{- else if in (slice false "false") ($.Page.Param $name) }}
{{- $value = false }}
{{- else }}
{{- $value = $.Page.Param $name }}
{{- end }}
{{- end }}
{{- end }}
{{- else }}
{{- errorf "The %s shortcode did not find %q in front matter. See %s" $.Name $name $.Position }}
{{- end }}
{{- $value -}}
This will return false
if the param is false
or "false"
, and will return true
if the param is true
or "true"
.
For my simple use case, the following might sufficient.
---
title: param test
bool1: false
bool2: true
str1: "str1"
str2: "false"
number1: 1
number2: 3.14
---
{{< param2.inline "bool1" >}}{{ $.Page.Param (.Get 0) }}{{< /param2.inline >}}
{{< param2.inline "bool2" />}}
{{< param2.inline "str1" />}}
{{< param2.inline "str2" />}}
{{< param2.inline "number1" />}}
{{< param2.inline "number2" />}}