Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trying to do reverse range in html/template... Is there a better way?
<ul>
{{/* So simple... */}}
{{range .Commits}}<li>{{.Commit.Message}}</li>
{{end}}
{{/* Is this really the shortest/best way to to do reverse range? */}}
{{range $i, $v := .Commits}}<li>{{(index $.Commits (revIndex $i (len $.Commits))).Commit.Message}}</li>
{{end}}
</ul>
funcMap := template.FuncMap{
"revIndex": func(index, length int) (revIndex int) { return (length - 1) - index },
}
@tutley

This comment has been minimized.

Show comment
Hide comment
@tutley

tutley Aug 28, 2017

I don't know if there's a better way, but I used yours and it worked. Thanks!

tutley commented Aug 28, 2017

I don't know if there's a better way, but I used yours and it worked. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment