"If or" and "if and" with .Section - need help making this DRYer

I feel like this is something I should be getting by this point, but…

I have a top bar section in a site I’m building that I want to show the section title if it’s in either the “tutorials” or “articles” section of the site. Right now, I have an if and if else statement that just repeats the same templating. Keep in mind this is at the page level (i.e., not on a list or node). I’ve tried the following with no luck:

{{if or (eq .Section "tutorials")(eq .Section "articles")}}
...here is some stuff to show in the top bar...

But I’m getting an incomplete template error in the console. I also tried with both “==” and moving the position of or. Since the top bar is used across every page, I thought maybe I needed to add a first if with .IsPage, but that still created the same error, which was a nonissue when I created the if/if else templating that is now working without issue. This is more of an attempt to make things drier than necessarily a hurdle to overcome. Thanks!

update Figured the working code might make this clearer:

@digitalcraftsman you tend to be my guru in this arena. Thoughts?

Hi @rdwatters,

I cloned your repo and tested your first approach with

{{ if or (eq .Section "tutorials") (eq .Section "articles") }}
// your prev / next links
{{ end }}

and everything worked fine. Note that you have to put a space between the parentheses (eq ...) (eq ...).

Otherwise please post the error message from the terminal.


Well, I guess I can’t feel too bad that I had the templating wrong, but missing a space? Bush league on my part, haha. All good now.

As always, thanks for your help, brother!

Great to hear that it worked. Such errors that can easily be overseen are the worst :slightly_smiling:

Maybe someone knows why this line is not working?

{{ if and ($authors := .Params.authors) (ne $authors "none") }}

I want to with condition $authors=none strings does not processed, but they continue to be processed, although {{ printf "%S" $authors }} shows [%!S(string=none)].

An assignment ($authors := .Params.authors) inside an if-statement doesn’t make sense.

What datatype is .Params.authors? Is it a list, map or just a string? Depending on this we could choose the right template funcs for the comparison.

.Params.authors is taxonomy and defined in the front matter as:

  - none

{{ printf “%S” .Params.authors }} shows: [%!S(string=none)]

Maybe it is wrong, but it works. If the $authors is not defined, strings inside the “If” are not processed. Variable $author also receives the correct value.

You can check if none is an element of the authors list with this snippet:

{{ with .Params.authors }}
  {{ if not (in . "none") }}
  // do some stuff
  {{ end }}
{{ end }}

But why do you not just check if the list is empty?

Thank @digitalcraftsman. This is working code:

{{ with $authors := .Params.authors }}
  {{ if not (in . "none") }}
  (авт. {{ delimit (first 1 $authors) "" }})
  {{ end }}
{{ end }}

How it looks:

Just a tip: you can’t use the %s sequence to print map/slice The error you get is “%!S”, which is telling you that the parameter is not a string. Use %v instead. Examples:

{{ printf "%v" .Params.authors }}
{{ printf "%+v" .Params.authors }}
{{ printf "%#v" .Params.authors }}

See fmt for more info.

1 Like