Skip to content

Templates

Michał Nowotnik edited this page Jun 16, 2022 · 2 revisions

Template syntax

You can find out how to create Go templates in the official documentation or in the various places on the internet.

A couple of examples:

  • returning a variable called item defined in a context. Its value is "foo":

    {{ .item }} will evaluate to foo

  • running a function to generate string with two items next to each other

    {{ printf "%s%s" .item }}

  • returning the first item in a list called lst

    {{ index .lst 0 }}

  • returning the value under key foo from a map someMap

    {{ index .someMap "foo" }}

Additional functions

Native Go template functions are very limited so fzshell also provides useful functions from the sprig project.

Additionally, the following functions are available:

cmd

The cmd function evaluates a single command (without creating a subshell).

{{ .cmd "echo" "foo" .item }} 

Returns "foo bar" if item == "bar".

cmdPipe

Similarly to cmd, but for commands that accept only stdin input. Like tr:

{{ .item | cmdPipe "tr" "o" "a" }}

Returns "faa" if item == "foo".

shell

Concatenates arguments and evaluates them in bash:

{{ .shell "echo " .item "a" " b" }}

produces "fooa b" if item == "foo".

listGet

Convenience function to get a value from a list in a chained call. Its troublesome to do with the native index function.

{{ .lst | listGet 0 }}

listGet

Convenience function to get a value from a map in a chained call. Its troublesome to do with the native index function.

{{ .foomap | mapGet "barKey" }}
Clone this wiki locally