How do you access a map in frontmatter using "with"

My TOML frontmatter:

+++
[links]
  [links.website]
    text = "somethinghuuge"
    url = "http://somethinghuuuge.com"
  [links.twitter]
   text = "therealdonaldtrump"
   url = "http://twitter.com/therealdonaldtrump"
  [links.instagram]
  text = ""
  url = ""
+++

In my layout:

{{with .Params.links.website}}
<a href="{{.url}}">{{.text}}</a>
{{end}}

{{with .Params.links.twitter}}
<a href="{{.url}}">{{.text}}</a>
{{end}}

{{with .Params.links.instagram}}
<a href="{{.url}}">{{.text}}</a>
{{end}}

The code above works, but I don’t want to generate the anchor link for instagram as it has an empty url.
I tried doing {{with .Params.links.instagram.url}} instead, but than I can only access {{.}} e.g. url and not {{.text}}.

Try this:

{{with .Params.links.instagram}}
{{ if ne .url "" }}<a href="{{.url}}">{{.text}}</a>{{end}}
{{end}}

However, I’m wondering if you could just do this:

{{ range .Params.links }}
{{ if ne .url "" }}<a href="{{.url}}">{{.text}}</a>{{end}}
{{ end }}
1 Like

You’re the man! The first option works best for my use-case.

Thanks.