I have this code:
{{ if ne $element (last 1 $items) }} •{{ end }}
that I thought would output • if the loop around it had more $items to process. But it outputs • regardless.
How am I using this wrong?
I have this code:
{{ if ne $element (last 1 $items) }} •{{ end }}
that I thought would output • if the loop around it had more $items to process. But it outputs • regardless.
How am I using this wrong?
When passed a slice, the collections.First and collections.Last functions return a slice of the first or last N items, even if N is 1.
Do this instead:
{{ $s := slice "a" "b" "c" "d" }}
{{ range $k, $v := $s }}
{{ if lt $k (sub (len $s) 1) }}
THERE'S MORE
{{ end }}
{{ end }}
That’s just what I needed. Thanks. Sometimes Go Templates are hard. ![]()
that one should also do
{{ $collection := slice 1 2 3 4 5}}
{{ if ne 4 (index (last 1 $collection) 0) }}
{{ warnf "not 4" }}
{{ else }}
{{ warnf "is 4" }}
{{ end }}
I’ve added a couple of dirt simple examples to the documentation, but I would have thought this was sufficient:
Returns the given collection, limited to the last N elements.
maybe the return value in the docs could be slice of any or any[]… just an idea.
dunno if index slice -1 for adressing from end of the slice could be a valuable addition.
As shown in examples, it can return a string as well.
oops. missed that.
then a string is a collection
collections.Last N COLLECTION
https://go.dev/blog/strings#what-is-a-string
In Go, a string is in effect a read-only slice of bytes.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.