# Conditional if else for a range

**URL:** <https://discourse.gohugo.io/t/conditional-if-else-for-a-range/29266>\
**Category:** support\
**Created:** [November 6, 2020, 3:40pm UTC](https://discourse.gohugo.io/t/conditional-if-else-for-a-range/29266 "2020-11-06T15:40:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexPop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexpop/32/12005_2.png) [@AlexPop](https://discourse.gohugo.io/u/AlexPop)\
**Post date:** [November 6, 2020, 3:40pm UTC](https://discourse.gohugo.io/t/conditional-if-else-for-a-range/29266/1 "2020-11-06T15:40:09Z")

</div>

I have to a get different config data to the {{ range }} depends of the page Type. And can’t realise how.  
I do something like bellow but it doesn’t work

```
{{- if eq $.Type "te" }}
  {{- $socials := .Site.Params.social_item_te -}}
{{ else }}
  {{- $socials := .Site.Params.social_item -}}
{{ end -}}
{{ range $socials }}
  {{- if .link -}}
  <a title="{{- .title -}}" href="{{- .link -}}" target="_blank" rel="noopener"><span class="icon-round themeicon themeicon-{{ .icon }}"></span></a>
  {{- end -}}
{{ end }}

```

What is the best approach, please?

> RROR 2020/11/06 20:36:11 Process: loading templates: “/Users/…/socials.html:6:1”: parse failed: template: partials/socials.html:6: undefined variable “$socials”

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [November 6, 2020, 3:45pm UTC](https://discourse.gohugo.io/t/conditional-if-else-for-a-range/29266/2 "2020-11-06T15:45:12Z")

</div>

You define a variable with `:=` and override it with `=`. I think you need a `$socials` outside of the if/else with `:=` and inside the if/else only use `=`.

Also - if you are ranging over it, you need to make sure it’s a slice/array/rangeable thingy. Depending on where you have this code the `.` might not be the root-`.` - Try `$.Site.Params.social_item`.

---

<div class="post-metadata">

**Author:** ![AlexPop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexpop/32/12005_2.png) [@AlexPop](https://discourse.gohugo.io/u/AlexPop)\
**Post date:** [November 6, 2020, 3:55pm UTC](https://discourse.gohugo.io/t/conditional-if-else-for-a-range/29266/3 "2020-11-06T15:55:36Z")

</div>

$.Site.Params.social\_item change nothing. And I cant understand how to realise this one:

> You define a variable with `:=` and override it with `=` . I think you need a `$socials` outside of the if/else with `:=` and inside the if/else only use `=` .

and yes, I have to take outside the variable. I do not know how. This code works fine but it’s ugly:

```
{{- $socials := .Site.Params.social_item -}}
{{- if eq $.Type "typoinlife" }}
  {{ $socials = .Site.Params.social_item_te }}
{{ end -}}
{{- range $socials }}
  {{- if .link -}}
  <a title="{{- .title -}}" href="{{- .link -}}" target="_blank" rel="noopener"><span class="icon-round themeicon themeicon-{{ .icon }}"></span></a>
  {{- end -}}
{{ end -}}

```

---

<div class="post-metadata">

**Author:** ![AlexPop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexpop/32/12005_2.png) [@AlexPop](https://discourse.gohugo.io/u/AlexPop)\
**Post date:** [November 6, 2020, 4:15pm UTC](https://discourse.gohugo.io/t/conditional-if-else-for-a-range/29266/4 "2020-11-06T16:15:03Z")

</div>

Just found solution here: [Inline conditional for variable's existence](https://discourse.gohugo.io/t/inline-conditional-for-variables-existence/14092)  
Thks @davidsneighbour too

```
{{- $socials := .Site.Params.social_item -}}
{{- if eq $.Type "te" }}
  {{ $socials = .Site.Params.social_item_te }}
{{ else }}
  {{ $socials = .Site.Params.social_item }}
{{ end -}}
{{- range $socials }}
  {{- if .link -}}
  <a title="{{- .title -}}" href="{{- .link -}}" target="_blank" rel="noopener"><span class="icon-round themeicon themeicon-{{ .icon }}"></span></a>
  {{- end -}}
{{ end -}}
```

---

<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:** [November 8, 2020, 4:15pm UTC](https://discourse.gohugo.io/t/conditional-if-else-for-a-range/29266/5 "2020-11-08T16:15:05Z")

</div>

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