limakzi
September 2, 2024, 4:24pm
1
Assuming a have a slice of dict:
{{ $shareIcons := slice
(dict "name" "twitter" "icon" "twitter.svg" "href" "https://twitter.com/query={{ .Permalink }}" )
(dict "name" "facebook" "icon" "facebook.svg" "href" "..." )
(dict "name" "linkedin" "icon" "linkedin.svg" "href" "..." )
}}
and I range through that this way:
{{ range $shareIcons }}
<a
href="{{ .href }}"
>
</a>
{{ end }}
then twitter.href
gets rendered as
WARN https://twitter.com/query={{ .Permalink }}
Why? Are there any other elegant ways to get .Permalink
in querystring
?
bep
September 2, 2024, 4:41pm
2
Use e.g. printf:
(dict "name" "twitter" "icon" "twitter.svg" "href" ( printf "https://twitter.com/query=%s" .Permalink )
1 Like
limakzi
September 2, 2024, 4:42pm
3
I did printf
trick and it worked, but inside me, I feel it tricky a little bit.
Is there any cleaner way?
bep
September 2, 2024, 4:47pm
4
There are several ways to concatenate in Go templates. printf
is, according to my taste, the cleanest and best options and is certainly much preferred before something that’s not working.
system
Closed
September 4, 2024, 4:47pm
5
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.