You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sonic/template/extension/tool.go

38 lines
733 B
Go

package extension
import (
"math/rand"
"time"
"github.com/go-sonic/sonic/template"
"github.com/go-sonic/sonic/util"
)
type toolExtension struct {
Template *template.Template
}
func RegisterToolFunc(template *template.Template) {
t := &toolExtension{
Template: template,
}
t.addRainbow()
t.addRandom()
}
// addRainbow 彩虹分页算法
func (t *toolExtension) addRainbow() {
rainbowPage := func(page, total, display int) []int {
return util.RainbowPage(page, total, display)
}
t.Template.AddFunc("rainbowPage", rainbowPage)
}
func (t *toolExtension) addRandom() {
rand.Seed(time.Now().UnixNano())
random := func(min, max int) int {
return min + rand.Intn(max-min)
}
t.Template.AddFunc("random", random)
}