# Conditional on array length

**URL:** <https://discourse.gohugo.io/t/conditional-on-array-length/1541>\
**Category:** support\
**Created:** [August 8, 2015, 10:12am UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541 "2015-08-08T10:12:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfig](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pfig/32/9857_2.png) [@pfig](https://discourse.gohugo.io/u/pfig)\
**Post date:** [August 8, 2015, 10:12am UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541/1 "2015-08-08T10:12:46Z")

</div>

Hi!

While trying to come up with a way of setting a link only if there is content for it, I came up with the following:

```
{{ $posts_item := "Articles" }}
{{ $posts := where .Data.Pages "Section" "post" | len | gt 0 }}
{{ if $posts }}
{{ $posts_item := `<a href="{{ .Site.BaseURL }}post">Articles</a>` }}
{{ end }}

```

This however throws the following error:

```
ERROR: 2015/08/08 reflect: call of reflect.Value.Type on zero Value in partials/footer.html

```

and `$posts_item` is set to “Articles”. Am I overlooking something obvious?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [August 8, 2015, 10:52am UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541/2 "2015-08-08T10:52:02Z")

</div>

There is a gotcha in Go templates re. scoping of variables. Search for Scratch here and in the doc,

---

<div class="post-metadata">

**Author:** ![pfig](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pfig/32/9857_2.png) [@pfig](https://discourse.gohugo.io/u/pfig)\
**Post date:** [August 9, 2015, 3:23am UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541/3 "2015-08-09T03:23:36Z")

</div>

Thank you, I noticed scratch mentioned in another post regarding counters, I will use that strategy.

Cheers!

---

<div class="post-metadata">

**Author:** ![pfig](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pfig/32/9857_2.png) [@pfig](https://discourse.gohugo.io/u/pfig)\
**Post date:** [August 9, 2015, 3:59am UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541/4 "2015-08-09T03:59:40Z")

</div>

Hmm, I get the same error with the following code:

```
        {{ $posts_item := "Articles" }}
        {{ $.Scratch.Set "posts" (where .Data.Pages "Section" "post" | len) }}
        {{ if (gt ($.Scratch.Get "posts") 0) }}
        {{ $posts_item := `<a href="{{ .Site.BaseURL }}post">Articles</a>` }}
        {{ end }}

```

If I do

```
        {{ $posts_item := "Articles" }}
        {{ $.Scratch.Set "posts" 0 }}
        {{ range first 1 (where .Site.Pages "Section" "post") }}
        {{ $.Scratch.Add "posts" 1 }}
        {{ end }}
        {{ if (gt ($.Scratch.Get "posts") 0) }}
        {{ $posts_item := `<a href="{{ .Site.BaseURL }}post">Articles</a>` }}
        {{ end }}

```

I still get “Articles” at the end, even though there are posts present (I use the exact same range statement to display the most recent posts).

---

<div class="post-metadata">

**Author:** ![pfig](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pfig/32/9857_2.png) [@pfig](https://discourse.gohugo.io/u/pfig)\
**Post date:** [August 9, 2015, 4:20am UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541/5 "2015-08-09T04:20:33Z")

</div>

Got it!

```
        {{ $.Scratch.Set "posts_item" "Articles" }}
        {{ if (gt (where .Site.Pages "Section" "post" | len) 0) }}
        {{ $.Scratch.Set "posts_item" `<a href="` }}
        {{ $.Scratch.Add "posts_item" .Site.BaseURL }}
        {{ $.Scratch.Add "posts_item" `post">Articles</a>` }}
        {{ end }}
        <li>{{ $.Scratch.Get "posts_item" | safeHTML}}</li>

```

Thanks!

---

<div class="post-metadata">

**Author:** ![pfig](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pfig/32/9857_2.png) [@pfig](https://discourse.gohugo.io/u/pfig)\
**Post date:** [August 9, 2015, 4:31am UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541/6 "2015-08-09T04:31:22Z")

</div>

One final question 🙂 The template now looks like

```
      <ul>
        {{ $.Scratch.Set "section" "Articles" }}
        {{ if (gt (where .Site.Pages "Section" "post" | len) 0) }}
        {{ $.Scratch.Set "section" `<a href="` }}
        {{ $.Scratch.Add "section" .Site.BaseURL }}
        {{ $.Scratch.Add "section" `post">Articles</a>` }}
        {{ end }}
        <li>{{ $.Scratch.Get "section" | safeHTML}}</li>
        {{ $.Scratch.Set "section" "Links" }}
        {{ if (gt (where .Site.Pages "Section" "link" | len) 0) }}
        {{ $.Scratch.Set "section" `<a href="` }}
        {{ $.Scratch.Add "section" .Site.BaseURL }}
        {{ $.Scratch.Add "section" `link">Links</a>` }}
        {{ end }}
        <li>{{ $.Scratch.Get "section" | safeHTML }}</li>
        {{ $.Scratch.Set "section" "Quotes" }}
        {{ if (gt (where .Site.Pages "Section" "quote" | len) 0) }}
        {{ $.Scratch.Set "section" `<a href="` }}
        {{ $.Scratch.Add "section" .Site.BaseURL }}
        {{ $.Scratch.Add "section" `quote">Quotes</a>` }}
        {{ end }}
        <li>{{ $.Scratch.Get "section" | safeHTML }}</li>
      </ul>

```

with the generated HTML becoming

```
      <ul>
        
        
        
        
        
        
        <li><a href="https://pfig.be/post">Articles</a></li>
        
        
        
        
        
        
        <li><a href="https://pfig.be/link">Links</a></li>
        
        
        
        
        
        
        <li><a href="https://pfig.be/quote">Quotes</a></li>
      </ul>

```

Surely there’s a better way to do this and I’m just being thick, right? It’s 5:30 in the UK 🙂

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [August 9, 2015, 3:51pm UTC](https://discourse.gohugo.io/t/conditional-on-array-length/1541/7 "2015-08-09T15:51:37Z")

</div>

I think what you see here is this issue:

> <https://github.com/golang/go/issues/9969>
>
> In many text/template scenarios, when creating text files (e.g. csv) for machine consumption, or text documents for human consumption, it's desirable...

In Hugo these extra newlines doesn’t impact the rendering, but they do look ugly. I have a HTML minifier in my build chain (Gulp task) to fix this (and other things).
