Conditional if else for a range

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”

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.

$.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 -}}

Just found solution here: Inline conditional for variable's existence
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 -}}

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