Hugo

Generating url from front matter in hugo template

Hi there I have a set of front matter variables defined in each article.

I would like to make a url out of the same which i can pass it to some thirdparty library

lets say i have a front matter like this

+++
name=mike
age=24
height=167
+++

i would like to generate this into a variable in hugo template (single)
?name=mike&age=24&height=167

In order to do this i tried various things like

{{ $someVar := "" }}
{{ range $index, $component := .Params }}
{{ $someVar := (print $someVar $index "=" $component "&") }}
{{ end }}

The problem is that $someVar is only available within the loop. Outside the loop its empty.

Kindly help.

Hi there. When inside the loop use = instead of :=

1 Like

Hi thanks for the reply,

It worked like a charm

I have one problem though

If i print the variable like this
{{ $somVar }} i get the expected output “name=mike&age=24&height=167”

but if i put the same as a parameter to a url like
link rel=‘https://chatbot.hellotars.com/conv/rkoHZD/?{{ $url }}’

it generates the value as
https://chatbot.hellotars.com/conv/rkoHZD/?name%3Dmike%26age%3D24%26height%3D167

I have done something thats not right

See if this helps

{{ $url | safeHTML }}
1 Like

Also, since you are joining each param on &, I would create an empty slice, then append the key=value on each loop, then use the delimit function for &. Merely personal preference though.

1 Like

I cant thank you enough
All my issues are solved.
Thank You very much

1 Like