# Conditional Variables

**URL:** https://discourse.gohugo.io/t/conditional-variables/44666
**Category:** support
**Created:** [June 2, 2023, 6:30pm UTC](https://discourse.gohugo.io/t/conditional-variables/44666 "2023-06-02T18:30:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jason\_Kennedy](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jason_kennedy/32/5735_2.png) [@Jason\_Kennedy](https://discourse.gohugo.io/u/Jason_Kennedy)
#### Post date: [June 2, 2023, 6:30pm UTC](https://discourse.gohugo.io/t/conditional-variables/44666/1 "2023-06-02T18:30:19Z")

</div>

Is there a way to conditionalize variables similar to the following:

‘’’  
{{ if eq .Page.Params.type “featured-artists” }}  
{{ $artistName := .Page.Params.Title }}  
{{ $artistURL := .Page.Params.artistURL }}  
{{ else }}  
{{ $artistName := .Page.Params.jurorGuestName }}  
{{ $artistURL := .Page.Params.jurorGuestURL }}

{{ end }}  
‘’’

This ability would greatly DRY out a lot of my code

Thnx

---

<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: [June 2, 2023, 6:34pm UTC](https://discourse.gohugo.io/t/conditional-variables/44666/2 "2023-06-02T18:34:58Z")

</div>

Yes. Initialize with `:=` before the conditional block, and assign with `=` within the conditional block.

```plaintext
{{ $a := 0 }}
{{ if true }}
  {{ $a = 6 }}
{{ else }}
  {{ $a = 7 }}
{{ end }}
{{ $a }} --> 6

```

---

<div class="post-metadata">

### Author: ![Jason\_Kennedy](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jason_kennedy/32/5735_2.png) [@Jason\_Kennedy](https://discourse.gohugo.io/u/Jason_Kennedy)
#### Post date: [June 2, 2023, 8:54pm UTC](https://discourse.gohugo.io/t/conditional-variables/44666/4 "2023-06-02T20:54:22Z")

</div>

Interesting, I thought for giggles to try out Bard, Googles Ai that now can answer coding questions and this is the answer given:

> …you can use the `cond` function to conditionally assign values to variables. The `cond` function takes three arguments: a condition, a value to return if the condition is true, and a value to return if the condition is false.
> 
> In your case, the condition would be whether the `type` parameter is equal to `"featured-artists"`. If it is, then the value of the `artistName` and `artistURL` variables would be set to the values of the `Title` and `artistURL` parameters, respectively. Otherwise, the values of the `artistName` and `artistURL` variables would be set to the values of the `jurorGuestName` and `jurorGuestURL` parameters, respectively.
> 
> Here is the code:

```auto
{{ $artistName := cond (eq .Page.Params.type "featured-artists") .Page.Params.Title .Page.Params.jurorGuestName }}

{{ $artistURL := cond (eq .Page.Params.type "featured-artists") .Page.Params.artistURL .Page.Params.jurorGuestURL }}

```

> This code would assign the following values to the `artistName` and `artistURL` variables:
> 
> - `artistName` = `Title` if the `type` parameter is equal to `"featured-artists"`
> - `artistName` = `jurorGuestName` if the `type` parameter is not equal to `"featured-artists"`
> - `artistURL` = `artistURL` if the `type` parameter is equal to `"featured-artists"`
> - `artistURL` = `jurorGuestURL` if the `type` parameter is not equal to `"featured-artists"`
> 
> You can then use the `artistName` and `artistURL` variables in your template as you would any other variable.

Not bad! Maybe send RP coding question to Bard first? LOL

@jmooring

---

<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: [June 2, 2023, 9:00pm UTC](https://discourse.gohugo.io/t/conditional-variables/44666/5 "2023-06-02T21:00:50Z")

</div>

The problem you can run into with the `cond` function is that it can only evaluate boolean values (`true` or `false`). It cannot evaluate other truthy or falsy values.

```plaintext
{{ warnf "%s" (cond 42 "6" "7") }}

Error ... executing "main" at <42>: expected bool; found 42

```
