I have been using Hugo for months now but I still don’t understand why some conditions work and others don’t. For example, {{- if and (ne .Section "lorem") (ne .Section "ipsum") }}
works but {{- if and (ne .Section (slice "lorem" "ipsum") ) }}
does not work.
You are comparing a scalar to a slice.
Use need to use the not
operator with the in
function.
Like this? {{- if not (in .Section (slice "lorem" "ipsum") ) }}
. Doesn’t work.
{{ $haystack := slice "a" "b" }}
{{ $needle := "c" }}
{{ if not (in $haystack $needle) }}
{{ printf "%s is not in %s" $needle $haystack }}
{{ end }}
You’ve reverse the args for in.
I tried this before posting. Didn’t work either. So, I am not sure what is up. I am more or less looking to shorten the ‘wordiness’ of the code {{- if and (ne .Section "lorem") (ne .Section "ipsum") }}
. But I am inclined to just retain it as it is.
{{- $sections := (slice "lorem" "ipsum") }}
{{- if not (in .Section $sections ) }}
{{- partial "dola.html" . }}
{{- end }}
If you are trying to make sure that the current section is neither "lorum nor “ipsum”…
reverse .Section and $sections
This is the second time (also faced this in a recent question), where reversing is required. Is there a section in the docs explaining this?
The order of args depends on the function.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.