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

35 lines
632 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() {
t.Template.AddFunc("rainbowPage", util.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)
}