last
Returns the last element of a slice, or the trailing 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 trailing byte of a string as a one-byte string.
Given
{"word": "hello"}Template
{{ word | last }}Result
oBasicSource
Reads the trailing 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 | last }}Result
oBasicSource
Returns the last element of a slice as-is.
Given
{"items": ["espresso", "latte", "macchiato"]}Template
{{ items | last }}Result
macchiatoNumbersSource
Returns the last element of a slice of numbers, keeping it a number.
Given
{"scores": [3, 1, 4, 1, 5]}Template
{{ scores | last }}Result
5EmptySource
Shows that last has nothing to return for an empty string, so it reports an error.
Given
{"word": ""}Template
{{ word | last }}Result
error: failed to render template: failed to render variable token 'word': modifier "last": function failed to apply: last expected a non-empty string