Template dictionary value

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?

Use e.g. printf:

(dict "name" "twitter" "icon" "twitter.svg" "href" ( printf "https://twitter.com/query=%s" .Permalink )

1 Like

I did printf trick and it worked, but inside me, I feel it tricky a little bit. :slight_smile:
Is there any cleaner way?

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.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.