join

Joins the elements of a slice into a single string.

Every element must be a string; a non-string element is an error. The separator defaults to a newline when it is omitted or empty.

InputArgumentsReturns[]anyanystring[]anyno argumentsstring
Source
BasicSource

Joins the elements of a slice into one string with the given separator.

Given

{"tags": ["coffee", "sale", "new"]}

Template

{{ tags | join:',' }}

Result

coffee,sale,new
PipeSource

Joins the elements with a pipe separator, a common way to render a slice as a single delimited field.

Given

{"parts": ["foo", "bar", "baz"]}

Template

{{ parts | join:' | ' }}

Result

foo | bar | baz
SpaceSource

Joins the elements of a slice with a multi-character separator.

Given

{"words": ["one", "two", "three"]}

Template

{{ words | join:' - ' }}

Result

one - two - three
BasicSource

Joins the elements of a slice on a newline when no separator is given.

Given

{"items": ["a", "b", "c"]}

Template

{{ items | join }}

Result

a
b
c
PairSource

Joins a two-element slice, placing each element on its own line since the default separator is a newline.

Given

{"items": ["line one", "line two"]}

Template

{{ items | join }}

Result

line one
line two
SingleSource

Joins a single-element slice, leaving it as just that element with no separator added.

Given

{"items": ["only"]}

Template

{{ items | join }}

Result

only
Non stringSource

Reports an error when a slice element is not a string.

Given

{"nums": ["1", 2, "3"]}

Template

{{ nums | join:',' }}

Result

error: failed to render template: failed to render variable token 'nums': modifier "join": function failed to apply: join expected an array of strings, got int at index 1