In HTML, you can have boolean attributes, an example is the checked
attribute of the input
element. According to the HTML specification, the presence of a boolean attribute on an element represents the true
value, and the absence of the attribute represents the false
value.
I tried to mimic a HTML like attribute with a Hugo shortcode, with partial success only.
What I did so far: I authored a shortcode checkbox.html
, this shortcode is supposed to get a html-like checked
attribute :
{{ with .Params }}
{{- if index . "checked" -}}
☑ Checked item
{{- else -}}
☑ Checked item
{{end}}
{{- else -}}
☐ Unchecked item
{{ end }}
I now use this shortcode in my pages like this:
{{% checkbox checked=true %}}
{{% checkbox checked="true" %}}
{{% checkbox %}}
and it correctly prints out
☑ Checked item
☑ Checked item
☐ Unchecked item
However, if I specify the checked
attribute as wanted (html like, without any value):
{{% checkbox checked %}}
I’m running into this error:
failed to process shortcode: "my\path\checkbox.html:2:9":
execute of template failed: template: shortcodes/checkbox.html:2:9:
executing "shortcodes/checkbox.html" at <index . "checked">:
error calling index: cannot index slice/array with type string
Any way to make this work?