{"id":"32526316911446eb","repo":"gohugoio/hugo","slug":"errwrongargstructure","errorCode":"errWrongArgStructure","errorMessage":"expected a map, a slice with an even number of elements, or an even number of scalar values, and each key must be a string","messagePattern":"expected a map, a slice with an even number of elements, or an even number of scalar values, and each key must be a string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/querify.go","lineNumber":25,"sourceCode":"//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\npackage collections\n\nimport (\n\t\"errors\"\n\t\"net/url\"\n\n\t\"github.com/gohugoio/hugo/common/hmaps\"\n\t\"github.com/spf13/cast\"\n)\n\nvar (\n\terrWrongArgStructure = errors.New(\"expected a map, a slice with an even number of elements, or an even number of scalar values, and each key must be a string\")\n\terrKeyIsEmptyString  = errors.New(\"one of the keys is an empty string\")\n)\n\n// Querify returns a URL query string composed of the given key-value pairs,\n// encoded and sorted by key.\nfunc (ns *Namespace) Querify(params ...any) (string, error) {\n\tif len(params) == 0 {\n\t\treturn \"\", nil\n\t}\n\n\tif len(params) == 1 {\n\t\tswitch v := params[0].(type) {\n\t\tcase map[string]any: // created with collections.Dictionary\n\t\t\treturn mapToQueryString(v)\n\t\tcase hmaps.Params: // project configuration or page parameters\n\t\t\treturn mapToQueryString(v)\n\t\tcase []string:\n\t\t\treturn stringSliceToQueryString(v)","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/collections/querify.go#L7-L43","documentation":"`collections.Querify` builds a URL query string and accepts exactly three input shapes: a single map, a single slice with an even number of elements, or an even number of scalar key/value arguments (tpl/collections/querify.go:31-64). `errWrongArgStructure` is returned when the single argument is some other type, or when the element/argument count is odd — because query strings are strictly key/value pairs.","triggerScenarios":"`{{ querify \"a\" 1 \"b\" }}` (odd argument count); `{{ querify (slice \"a\" \"1\" \"b\") }}` (odd-length slice); `{{ querify .Page }}` or any single argument that isn't a map or slice, such as a plain string or number.","commonSituations":"Building pagination or filter links and forgetting a value for the last key; passing a raw query string (\"a=1&b=2\") expecting parsing; passing `.Params` values of unexpected shape; templates that append conditionally to a slice and end up with an odd length.","solutions":["Count your arguments: every key needs a value — `querify \"a\" 1 \"b\" 2`.","When building the input dynamically, use a dict instead of a slice so pairing is enforced: `querify (dict \"a\" 1 \"b\" 2)`.","If passing one argument, make sure it's a map or an even-length slice, not a pre-built string.","Debug with `{{ warnf \"%v\" $args }}` to inspect the actual shape at build time."],"exampleFix":"<!-- before: odd number of args -->\n{{ querify \"page\" 2 \"sort\" }}\n<!-- after -->\n{{ querify \"page\" 2 \"sort\" \"date\" }}","handlingStrategy":"validation","validationCode":"{{/* querify accepts: a map, a slice with even length, or even scalar args with string keys */}}\n{{ $kv := dict \"page\" 1 \"sort\" \"date\" }}\n{{ $q := querify $kv }}\n{{/* if using a slice: */}}\n{{ if ne (mod (len $pairs) 2) 0 }}{{ errorf \"querify: odd number of elements\" }}{{ end }}","typeGuard":"{{ $ok := true }}\n{{ range $i, $v := $pairs }}\n  {{ if and (eq (mod $i 2) 0) (not (reflect.IsString $v)) }}{{ $ok = false }}{{ end }}\n{{ end }}","tryCatchPattern":null,"preventionTips":["Prefer passing a dict to querify — it sidesteps both the even-count and key-type rules","When passing pairs, verify even length and that every even-index element is a string","Beware numeric front-matter values used as keys — convert with `string` first"],"tags":["hugo","templates","collections","querify","url"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}