Isset in range

Hi
I am struggling with the problem how to check if a variable exists (isset) within a range.

Example: I want to show a name and possibly a link for some group of people.

My data is as follows: data/my_page.yml (ie. ABC has a link, but XYZ has no link)

data:
  - name: ABC
    link: https://example.com
  - name: XYZ

And my template is:

{{ range .Site.Data.my_page.data }}
    {{ .name }}
    IF LINK EXISTS FOR THE RANGE ITEM:
        <a href="{{ .link }}>Link</a>
    END
{{ end }}

Thanks!

with is your friend here :slight_smile:

{{ range .Site.Data.my_page.data }}
    {{ with .link }}
      <a href="{{.}}">link</a>
    {{ end }}

    // if you really want to use isset: 
    {{ if isset . "link" }}
      <a href="{{.link}}">link</a>
    {{ end }}
{{ end }}
2 Likes