first

Returns the first element of a slice, or the leading byte of a string.

An empty string is a miss the default modifier can catch.

InputArgumentsReturnsstringno argumentsstring[]byteno argumentsstring[]anyno argumentsany
Source

Behaviors

Catchable miss
When the data it looks for is not there, it reports a miss rather than failing the render, so default can supply a fallback and an if or for reads the absence as false.
BasicSource

Returns the leading byte of a string as a one-byte string.

Given

{"word": "hello"}

Template

{{ word | first }}

Result

h
BasicSource

Reads the leading byte of a []byte buffer as text, so a file read or an HTTP body behaves the same as a string rather than yielding a raw byte number from the slice clause.

Given

{"buffer": []byte("hello")}

Template

{{ buffer | first }}

Result

h
BasicSource

Returns the first element of a slice as-is, keeping its type.

Given

{"items": ["espresso", "latte", "macchiato"]}

Template

{{ items | first }}

Result

espresso
NumbersSource

Returns the first element of a slice of numbers, leaving it a number rather than turning it into text.

Given

{"scores": [3, 1, 4, 1, 5]}

Template

{{ scores | first }}

Result

3
EmptySource

Shows that first has nothing to return for an empty string, so it reports an error.

Given

{"word": ""}

Template

{{ word | first }}

Result

error: failed to render template: failed to render variable token 'word': modifier "first": function failed to apply: first expected a non-empty string