I have a shortcode that accepts a variable number of distinct parameters that are formatted and sorted. When sorted, certain items are placed at the front in a particular order, and all others are added to the end in the order they were received.
I have this working with the following code. Duplicate parameters are removed by uniq, and the remaining parameters are pushed through a partial that may reformat the text. The partial returns a string, and the result is appended to $keys. Finally, the intersect takes $modifiers (in the order they should appear in the final results) and $keys so that a collection of matching modifiers is created in the desired order. That collection is then passed to union along with $keys.
This code works:
{{- $modifiers := slice "Ctrl" "LCtrl" "RCtrl" "Alt" "LAlt" "RAlt" "Opt" "LOpt"
"ROpt" "Shift" "LShift" "RShift" "Win" "LWin" "RWin" "Cmd" "LCmd" "RCmd" "Fn" }}
{{- $keys := (slice) }}
{{- range .Params | uniq }}
{{- $keys = $keys | append (partial "func/getKey.html" (. | safeHTML)) }}
{{- end }}
{{- $sorted := union (intersect $modifiers $keys) $keys -}}
Having read the docs on apply, I thought this code might simply things and replace the range loop with a a single statement:
{{- $modifiers := slice "Ctrl" "LCtrl" "RCtrl" "Alt" "LAlt" "RAlt" "Opt" "LOpt"
"ROpt" "Shift" "LShift" "RShift" "Win" "LWin" "RWin" "Cmd" "LCmd" "RCmd" "Fn" }}
{{- $keys := apply (.Params | uniq) "partial" "func/getKey.html" "." -}}
{{- $sorted := union (intersect $modifiers $keys) $keys -}}
For some reason, I receive an error with the union stating that “value of type interface {} is not assignable to type string”. I have no idea what that means, and the web searches I completed turned up nothing useful.
at <union (intersect $modifiers $keys) $keys>: error calling union: reflect.Set: value of type interface {} is not assignable to type string
I tried simplifying things more just to get it to compile:
{{- $sorted := apply .Params "partial" "func/getKey.html" "." -}}
But now I receive a different error.
at <apply .Params “partial” “func/getKey.html” “.”>: error calling apply: partial that returns a value needs a non-zero argument.
Is something wrong with my partial?
Here’s a truncated version of “func/getKey.html”:
{{ $key := lower . }}
{{ if (eq $key "control") }}{{ $key = "Ctrl" }}
{{ else if (eq $key "option") }}{{ $key = "Opt" }}
{{ else if (eq $key "command") }}{{ $key = "Cmd" }}
{{ else if (eq $key "windows") }}{{ $key = "Win" }}
{{ else if (eq $key "function") }}{{ $key = "Fn" }}
{{ else if eq $key "escape" }}{{ $key = "Esc" }}
---//---
{{ else }}{{ $key = title $key }}{{ end }}
{{ return printf "%s" $key}}
I’m running Hugo server hugo v0.85.0-724D5DB5+extended windows/amd64 BuildDate=2021-07-05T10:46:28Z VendorInfo=gohugoio with the following arguments “-D --disableFastRender --bind={local IP} --baseURL={url} -p {port}” I’m going to see if a new build changes this behavior.