HUGO

How get data by changing the name of the variable?

Hello.
I am a beginner user HUGO.
How can I get dynamic name variable?

<!-- config.toml -->
[[params.location]]
country = "Nairobi"
in_cdn = 1
avail_cdn = 0

<!--index.html  -->
{{ $list := .Site.Params.location }} <!-- DATA params -->
{{ $param := "cdn" }} 

{{ range $index, $value := first 10 $list }}
   {{ if eq .in_($param) 1 }}   ---> I want to receive next: {{ if eq .in_cdn 1 }}
   ...code
   {{ end }}
{{ end }}

Screenshot - Joxi (118 kb) закачан 15 июля 2021 г. Joxi

You would use the index function: index | Hugo

Something like:

{{ range $index, $value := first 10 $list }}
  {{ $p := print "in_" $param }}
  {{ index $value $p }}
{{ end }}

You may need to wrap it in an if isset: isset | Hugo

{{ range ...
  {{ if isset $value $p }}
    {{ index $value $p }}

1 Like

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