HUGO

Checking params for full array

Greeting,
I am trying to figure out how to prevent output when I have an incomplete array. I think my use case is like this one, but where as they are looking at the whole array as being empty while I am requiring the whole array to be full.

My data (a page level param— in the header) is structured like (A) & (B) below. I am creating an output like the following:

<dc:contributor xsi:type="olac:role" olac:code="{{ .role | lower }}">{{- .name -}}</dc:contributor>

However I want this line to only be passed to the output when there is both .role and .name. I am currently using the code in ( C) to range over data like (B) an get one output per .role. Put another way there should be 5 lines of output for (B) and zero lines of output for (A).

A.

olac_roles:
- role:
  name: Susan

B.

olac_roles:
- role: researcher
  name: tom
- role: speaker
  name: bill
- role: annotator
  name: susan
- role: transcriber
  name: sally
- role: translator
  name: george

C.

{{- range .Params.olac_roles -}}
   <dc:contributor xsi:type="olac:role" olac:code="{{ .role | lower }}">
                {{- .name -}}
            </dc:contributor>
    {{- end -}}

I have also tried some combinations of if statements (and & or & ne & eq) but I’m missing something. The behavior I am seeing regardless of the details of the if clause is that the whole output is skipped, even in cases like (B).

{{- range .Params.olac_roles -}}
   {{- if and (ne .role "" ) (eq .name "" ) -}}
   <dc:contributor xsi:type="olac:role" olac:code="{{ .role | lower }}">
                {{- .name -}}
            </dc:contributor>
        {{- end -}}
    {{- end -}}

any pointers are appreciated. i.e. do I want a nested where clause?

I want this line to only be passed to the output when there is both .role and .name

{{ if ne (or (ne .role "") (ne .name "")) }}
// will be executed if neither role nor name is not set
// if not (ne) either (role is empty) or (name is empty)
{{ end }}

should do that specific trick. It sometimes helps to spell things out and start reading the code from the back right to left.

  • if .name is empty “or” will be true and fail the ne
  • if .role is empty “or” will be true and fail the ne
  • if both are NOT empty the “or” will be false and ne won’t fail.

Hi. Cond function is a simple way using var1 or var2 depending on condition argument.

{{ cond CONDITION var1 var2 }}