Work with conditionals in gohugo

Working with conditionals in gohugo is really different than working with them in like java, C# or any other language.

Base concept #

When you want to work with conditionals in gohugo you have to keep in mind that you work with functions.

javascript

if (a == b) {
  // Do something
}

gohugo

{{ if eq a b }}
    <h1>eq is a function that takes 2 parameters to compare</h1>
{{ end }}

If #

A simple if is formed like follows

{{ if ... }}
    <h1>simple if</h1>
{{ end }}

If else #

A if/else is formed like follows

{{ if ... }}
    <h1>...</h1>
{{ else }}
    <h1>...</h1>
{{ end }}

If else if #

A if/elseif is formed like follows

{{ if eq a b }}
    <h1>...</h1>
{{ else if eq c d }}
    <h1>...</h1>
{{ end }}

Conditionals function #

eq -> Equal #

This function takes two parameters. The condition is fullfilled arg1 is equal to arg2.

{{ a := "/" }}
{{ b := "/blob/" }}
{{ if eq a b }}
    <h1>The condition is fullfilled</h1>
{{ else }}
    <h1>The condition is not fullfilled</h1>
{{ end }}

ne -> Not equal #

This function takes two parameters. The condition is fullfilled only if arg1 is not equal to arg2.

{{ a := "/" }}
{{ b := "/" }}
{{ if ne a b }}
    <h1>The condition is fullfilled</h1>
{{ else }}
    <h1>The condition is not fullfilled</h1>
{{ end }}

gt -> Greater than #

This function takes two parameters. The condition is fullfilled only if arg1 is greater than arg2.

{{ a := 2 }}
{{ b := 1 }}
{{ if gt a b }}
    <h1>The condition is fullfilled</h1>
{{ else }}
    <h1>The condition is not fullfilled</h1>
{{ end }}

lt -> Less than #

This function takes two parameters. The condition is fullfilled only if arg1 is less than arg2.

{{ a := 1 }}
{{ b := 2 }}
{{ if lt a b }}
    <h1>The condition is fullfilled</h1>
{{ else }}
    <h1>The condition is not fullfilled</h1>
{{ end }}

le -> Less or equal than #

This function takes two parameters. The condition is fullfilled only if arg1 is less or equal than arg2.

{{ a := 1 }}
{{ b := 1 }}
{{ if lt a b }}
    <h1>The condition is fullfilled</h1>
{{ else }}
    <h1>The condition is not fullfilled</h1>
{{ end }}

ge -> Greater or equal than #

This function takes two parameters. The condition is fullfilled only if arg1 is greater or equal than arg2.

{{ a := 1 }}
{{ b := 2 }}
{{ if ge a b }}
    <h1>The condition is fullfilled</h1>
{{ else }}
    <h1>The condition is not fullfilled</h1>
{{ end }}

Operators and how to use them #

and -> And #

This function takes two parameters. This function returns true only if its two parameters are true.

{{ a := True }}
{{ b := False }}
{{ if and a b }}
    <h1>This will never show up</h1>
{{ else }}
    <h1>...</h1>
{{ end }}

equivalent in js

if (true && false) {
  // ...
}

Combine and & eq

{{ a := "/" }}
{{ b := "/blog/" }}
{{ c := "/projects/" }}
{{ d := "/resume/" }}
{{ if and (eq a b) (eq b c) }}
    <h1>This will never show up</h1>
{{ else }}
    <h1>...</h1>
{{ end }}

equivalent in js

if ("/" == "/blog/" && "/projects" == "/resume") {
  // ...
}

or -> Or #

This function takes two parameters. This function returns true if on of its two parameters is true.

{{ a := True }}
{{ b := False }}
{{ if or a b }}
    <h1>Showing up</h1>
{{ else }}
    <h1>...</h1>
{{ end }}

equivalent in js

if (true || false) {
  // ...
}

Combine and & eq

{{ a := "/" }}
{{ b := "/" }}
{{ c := "/projects/" }}
{{ d := "/resume/" }}
{{ if or (eq a b) (eq b c) }}
    <h1>Showing up</h1>
{{ else }}
    <h1>...</h1>
{{ end }}

equivalent in js

if ("/" == "/" || "/projects" == "/resume") {
  // ...
}