Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/templateFunctions.go
akuma06 634f48a748 Big Update !
Design copied from NyaaTorrents
Reorganised some sql queries
Added dynamic navigation within torrents
Added nesting templates support
Added more variables/functions imported in the templates
Separated templates functions and variables in other files for more clarity
2017-05-05 03:53:38 +02:00

50 lignes
2 Kio
Go

package main
import(
"math"
"html/template"
"net/url"
"strconv"
"log"
)
var funcMap = template.FuncMap{
"min": math.Min,
"genRoute": func (name string, params ...string) string {
url, err := router.Get(name).URL(params...)
if (err == nil) {
return url.String()
}
return "error"
},
"genNav" : func (nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML {
maxPages := math.Ceil(float64(nav.TotalItem)/float64(nav.MaxItemPerPage))
route := router.Get(nav.Route)
var ret = ""
if nav.CurrentPage-1 > 0 {
url, _ := route.URL("page", "1")
ret = ret + "<li><a id=\"page-prev\" href=\""+ url.String() + "?" + currentUrl.RawQuery +"\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</span></a></li>"
}
startValue := 1
if (nav.CurrentPage > pagesSelectable) {
startValue = (int(math.Min((float64(nav.CurrentPage) + math.Floor(float64(pagesSelectable)/2)), maxPages))-pagesSelectable+1)
}
endValue := (startValue+pagesSelectable-1)
if (endValue > int(maxPages)) { endValue = int(maxPages) }
log.Println(nav.TotalItem)
for i := startValue; i <= endValue; i++ {
pageNum := strconv.Itoa(i)
url,_ := route.URL("page", pageNum)
ret = ret + "<li"
if (i == nav.CurrentPage) { ret = ret + " class=\"active\"" }
ret = ret + "><a href=\""+ url.String() + "?" + currentUrl.RawQuery +"\">"+strconv.Itoa(i)+"</a></li>"
}
if nav.CurrentPage < int(maxPages) {
url, _ := route.URL("page", strconv.Itoa(nav.CurrentPage+1))
ret = ret + "<li><a id=\"page-next\" href=\""+ url.String() + "?" + currentUrl.RawQuery +"\" aria-label=\"Next\"><span aria-hidden=\"true\">&raquo;</span></a></li>"
}
return template.HTML(ret)
},
}