funcs.go 440 B

12345678910111213141516171819202122232425
  1. package templates
  2. import "text/template"
  3. var AssistFunctions template.FuncMap
  4. func RegisterFunction(name string, function interface{}) {
  5. if AssistFunctions == nil {
  6. AssistFunctions = map[string]interface{}{}
  7. }
  8. AssistFunctions[name] = function
  9. }
  10. func Dec(val int) int {
  11. return val - 1
  12. }
  13. func ShortHand(val string) string {
  14. return val[:6]
  15. }
  16. func init() {
  17. RegisterFunction("dec", Dec)
  18. RegisterFunction("shorthand", ShortHand)
  19. }