# If statement not functioning

**URL:** <https://discourse.gohugo.io/t/if-statement-not-functioning/35122>\
**Category:** support\
**Created:** [October 13, 2021, 3:28am UTC](https://discourse.gohugo.io/t/if-statement-not-functioning/35122 "2021-10-13T03:28:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdamXweb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/adamxweb/32/15386_2.png) [@AdamXweb](https://discourse.gohugo.io/u/AdamXweb)\
**Post date:** [October 13, 2021, 3:28am UTC](https://discourse.gohugo.io/t/if-statement-not-functioning/35122/1 "2021-10-13T03:28:54Z")

</div>

Having trouble with Hugo getting this logic to work.

Am trying to set a thumbnail for the list view based on some logic:

- If there is a thumbnail show that (works)
- If no thumbnail but an image, then show image (works)
- if there isn’t an image, then show an image that corresponds with the category. (the issue below)

The category can occasionally have two values, so the frontmatter looks like this:

```auto
categories:
- number1
- number2

```

Or it could occasionally have just one value

```auto
categories:
- one

```

Have tried using some logic to test to see if it is a string or if it is an array if the front matter looks like the below, but the logic just gets bypassed.

```auto
categories: one

```

Inside the logic it sets the .Params.categories to a value, however I receive a build error if the value isn’t defined outside of the logic.

For example

```auto
    {{ if .Params.categories}}
      {{ $category := index (.Params.categories) 0}}
    {{else}}
      {{ $category:= ""}}
    {{end}}

```

Am I wrong thinking that if there is no category it should default $category to nothing?

Have also tried

```auto
    {{ $category:= ""}}
    {{ $cat := string .Params.categories}}
    {{ if eq (printf "%T" $cat) "string" }}
      {{ $category := $cat}}
    {{ else }}
      {{ $category := index ($cat) 0 }}
    {{end}}

```

However result always is “” and it never hits the logic.

Also went down the route of

```auto
    {{ $cat := .Params.categories}}
    {{ $type := (printf "%T" $cat) }}
    {{ $typeIsString := (findRE "^(string|template\\.(CSS|HTML|HTMLAttr|JS|JSStr|URL))$" $type) }}
    {{ $typeIsSlice := (findRE "^([[][]]|.*TaxonomyList|output\\.Formats|resource\\.Resources|.*navigation\\.Menu$|\\*?hugolib\\.Pages$|hugolib\\.OrderedTaxonomy$|hugolib\\.WeightedPages)" $type) }}
    {{if $typeIsSlice}}
      {{ $category := index ($cat) 0 }}
    {{ else if $typeIsString}}
      {{ $category := $cat}}
    {{ else }}
      {{ $category := "bob"}}
    {{end}}

```

However $category will never show bob and the page build error with

```auto
undefined variable "$category"

```

This logic is all within an else if statement if that makes any difference.  
Appreciate any help!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 13, 2021, 4:15am UTC](https://discourse.gohugo.io/t/if-statement-not-functioning/35122/2 "2021-10-13T04:15:20Z")

</div>

Use `:=` to initialize a variable.  
Use `=` to change its value.

```nohighlight
{{ $a := "" }}
{{ if true }}
  {{ $a = "foo" }}
{{ else }}
  {{ $a = "bar" }}
{{ end }}

{{ $a }} --> foo

```

[https://pkg.go.dev/text/template#hdr-Variables](https://pkg.go.dev/text/template#hdr-Variables)

---

<div class="post-metadata">

**Author:** ![AdamXweb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/adamxweb/32/15386_2.png) [@AdamXweb](https://discourse.gohugo.io/u/AdamXweb)\
**Post date:** [October 13, 2021, 4:32am UTC](https://discourse.gohugo.io/t/if-statement-not-functioning/35122/3 "2021-10-13T04:32:41Z")

</div>

Lifesaver, thank you!

Knew I was missing something.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [October 15, 2021, 4:33am UTC](https://discourse.gohugo.io/t/if-statement-not-functioning/35122/4 "2021-10-15T04:33:15Z")

</div>

Closed on October 15, 2021, 4:33am UTC

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