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.
InputArgumentsReturns
Sourcestringno argumentsstring[]byteno argumentsstring[]anyno argumentsanyBehaviors
- Catchable miss
- When the data it looks for is not there, it reports a miss rather than failing the render, so
defaultcan supply a fallback and aniforforreads the absence as false.
BasicSource
Returns the leading byte of a string as a one-byte string.
Given
{"word": "hello"}Template
{{ word | first }}Result
hBasicSource
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
hBasicSource
Returns the first element of a slice as-is, keeping its type.
Given
{"items": ["espresso", "latte", "macchiato"]}Template
{{ items | first }}Result
espressoNumbersSource
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
3EmptySource
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