Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpl/collections: Allow querify to accept a map argument #13131

Closed
jmooring opened this issue Dec 11, 2024 · 3 comments · Fixed by #13140
Closed

tpl/collections: Allow querify to accept a map argument #13131

jmooring opened this issue Dec 11, 2024 · 3 comments · Fixed by #13140
Milestone

Comments

@jmooring
Copy link
Member

jmooring commented Dec 11, 2024

The collections.Querify template function accepts either of the following argument forms:

{{ collections.Querify "a" 1 "b" 2 }}         --> a=1&b=2
{{ collections.Querify (slice "a" 1 "b" 2) }} --> a=1&b=2

But it would convenient if we could do:

{{ collections.Querify (dict "a" 1 "b" 2) }}

For example, in our embedded youtube shortcode we:

  1. Build a map of parameters

  2. Convert the map into a slice with this construct:

    {{ range $k, $v := $params }}
      {{ $s = $s | append $k }}
      {{ $s = $s | append $v }}
    {{ end }}
    
  3. Pass the slice to collections.Querify

It would be more elegant if we could remove step 2 above.

@bep
Copy link
Member

bep commented Dec 11, 2024

{{ collections.Querify (dict "a" 1 "b" 2) }}

We need to remember to sort the keys.

@jmooring
Copy link
Member Author

We need to remember to sort the keys.

The existing code calls Encode before returning the result.

Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.

So unless I'm missing something, I think that covers the sort.

@bep
Copy link
Member

bep commented Dec 11, 2024

So unless I'm missing something, I think that covers the sort.

You're right, I didn't look at the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants