Creating Key/Value pairs in front matter

I have the following parameters in my markdown

    - mark: ""
    - markh: 0.6
    - markalign: "middle,center"

I’m trying to pass this information to the template page where i use the querify function, which requires key/value pairs, however, this parameter is passed as map.

What is the best way to convert this to key/value pairs?

I tried some hackery using printf to generate a big string of key/value pairs, but then I am given the following error: “querify keys must be strings”

Any assistance here would be remarkabe. Thank you.


How are you passing the params to querify ?

Trying the following:

{{ querify .Params.imgix }}

Your .Params.imgix itself is an array of maps, and querify wants (from the docs):

querify KEY VALUE [KEY VALUE]...

… what seems to be like string pairs.

You could try to do something like

{{if isset .Params "imgix"}}
    {{ $.Scratch.Set "queries" slice }}
    {{ range .Params.imgix }} <!-- get all pairs -->
      {{ range $k, $v := . }} <!-- get each key $k and value $v -->
        {{ $.Scratch.Add "queries" (querify $k $v) }} <!-- collect querify-d -->
      {{ end }}
    {{ end }}
    <!-- assuming you want it all in one string? -->
    {{ delimit ($.Scratch.Get "queries") "&"}} 

Maybe someone else can suggest a more elegant solution though…

1 Like

@pointyfar, this is quite perfect! Plenty elegant enough for what I’m working.

I had started walking down the logic of something similar to that last night, but didn’t quite get there.

Thanks for the help.