Not in statement behavior


#1

The following works as expected.

{{range where .Site.RegularPages "Section" "books"}}
{{if not (in .URL "amazon")}} --> true
{{.Title}}
{{end}}
{{end}}

However, if I do the following as I don’t want to hardcode “amazon”, the logic stops working.

{{range where .Site.RegularPages "Section" "books"}}
{{if not (in .URL .File)}} --> false. should be true
{{.Title}}
{{end}}
{{end}}

{{.File}} = amazon

{{.URL}} = /books/amazon/

Another attempt:

{{range where .Site.RegularPages "Section" "books"}}
{{if eq (urlize (delimit .Keywords "")) "amazon"}}true{{else}}false{{end}} --> this give true
{{if not (in .URL (urlize (delimit .Keywords "")))}} --> this gives false, should be true
{{.Title}}
{{end}}
{{end}}

Any attempt to insert a variable that is identical to “amazon” will not work in the not in construct. Why is this?


#2

Have you confirmed the value of .File by printing it?

{{ .File }}

#3

Yes.

{{.File}} gives amazon.

I’ve even tried doing {{string .File}}


#4

Hmm. Not sure what’s going on then. Maybe others can help you.


#5

.File may show a string when printed but I don’t think it’s a string type, so in check its content as a map or slice. You should check File Variables and try with one of the available key, .File.BaseFileName or something.


#6

Thanks. Tried a bunch of different file variables, but nothing has worked so far.

also tried doing this:

{{if not (in .URL (string .File))}}

Appreciate the help and suggestions.


#7

Do you have a repo?


#8

Unfortunately, not that I can share which makes troubleshooting harder.

But the logic is sound right? If I add a new parameter to front matter, such as test = "amazon" and change the logic statement to

{{if not (in .URL .Params.test))}}

it works and shows the correct result. Only when I try to manipulate an existing variable, such as a file variable or even .Title and put it in the logic statement, it fails to show the correct result.


#9

.File is a complex object, not a string. You should stick to the variables that is documented for Page (URL is not on that list, it is considered internal and could be removed without notice):