Last active
August 28, 2017 19:23
-
-
Save dmitshur/5f9e93c38f6b75421060 to your computer and use it in GitHub Desktop.
Trying to do reverse range in html/template... Is there a better way?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
funcMap := template.FuncMap{ | |
"revIndex": func(index, length int) (revIndex int) { return (length - 1) - index }, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know if there's a better way, but I used yours and it worked. Thanks!