Understanding Complex Pipes (|)

I am trying to understand the use of | in an example in Create Your Own Shortcodes:

or .Get "title" | .Get "alt" | if

I would guess that this is the same as:

if or .Get "title"  .Get "alt"

or

if or  .Get "alt" .Get "title"

which are essentially the same end result.

How is the previous pipe evaluated step by step to arrive at the above?

This example:

{{ or .Get "title" | .Get "alt" | if }}

from https://gohugo.io/templates/shortcode-templates/#access-parameters is incorrect, and should either be removed from the documentation or changed to:

{{ if .Get "title" | or (.Get "alt") }}

which is equivalent to:

{{ if or (.Get "title") (.Get "alt") }}

which is, in my opinion, easier to read and understand.

Additional references:

From the go docs:

A pipeline may be “chained” by separating a sequence of commands with pipeline characters ‘|’. In a chained pipeline, the result of each command is passed as the last argument of the following command. The output of the final command in the pipeline is the value of the pipeline.

1 Like

Submitted issue and pull request to docs repo.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.