All Articles

Get data type in Hugo framework

I had a task where I need to output something based on the data type of the variable.

Basically, it goes something like this

   <div class="col-6">
        {{ if eq (printf "%T" . ) "float64" }}
          {{ . }}
        {{ else if eq (printf "%T" . ) "bool"}}
          {{ $translations.Get (string .)}}
        {{ else }}
        {{ .}}
        {{ end }}
    </div>

If the variable is a float64 display the variable as is. If it is a boolean display it by using a translation.

Next time you need to check the data type of your variable, just use this:

printf "%T" $yourVariableHere

If you’re a Golang expert, you probably already know this.I just hope this could help someone who’s new in Hugo.

TIL