I encountered an issue where an if conditional is not properly evaluating a variable if it is not passed as a quoted string. Best to explain with some simple code:
The following works:
{{ range …whatever… }}
{{ $.Scratch.Add “Draft” “false” }}
{{ end }}
…some html…
{{ $draft := $.Scratch.Get “Draft” }}
{{ $draft }} (properly outputs the word ‘false’)
{{ if eq “false” $draft }}
I AM FALSE
{{ else }}
I AM TRUE
{{ end }}
As expected, the output is I AM FALSE.
However, if I change the Scratch.Add to grab a variable that is available from the page front matter, it will output it correctly but not evaluate it: (assume .md has draft = “false”)
{{ range …whatever… }}
{{ $.Scratch.Add “Draft” .Draft }}
{{ end }}
…some html…
{{ $draft := $.Scratch.Get “Draft” }}
{{ $draft }} (properly outputs the word ‘false’)
{{ if eq “false” $draft }}
I AM FALSE
{{ else }}
I AM TRUE
{{ end }}
even though {{ $draft }} returns 'false, the conditional returns “I AM TRUE”.
Q1. Why?
Q2. Is this a bug that I should be reporting?
I have found a workaround (see below for working code) but curious why if fails when obviously the value is defined.
working work-around:
{{ range …whatever… }}
{{ if eq “true” .Draft }}
{{ $.Scratch.Add “Draft” “true” }}
{{ else }}
{{ $.Scratch.Add “Draft” “false” }}
{{ end }}
{{ end }}
… html code …
{{ $draft := $.Scratch.Get “Draft” }}
{{ $draft }} (properly displays value defined in range)
{{ if eq “false” $draft }}
I AM FALSE
{{ else }}
I AM TRUE
{{ end }}
(condition properly output expected value)
PS - Using Hugo Static Site Generator v0.19 linux/amd64 BuildDate: 2017-02-27T06:38:34-06:00
Regards,