template.go 925 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package utils
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "github.com/valyala/fasttemplate"
  6. )
  7. /*
  8. Web Template Generator
  9. This is the main system core module that perform function similar to what PHP did.
  10. To replace part of the content of any file, use {{paramter}} to replace it.
  11. */
  12. func Templateload(filename string, replacement map[string]interface{}) (string, error) {
  13. content, err := ioutil.ReadFile(filename)
  14. if err != nil {
  15. return "", nil
  16. }
  17. t := fasttemplate.New(string(content), "{{", "}}")
  18. s := t.ExecuteString(replacement)
  19. return string(s), nil
  20. }
  21. func TemplateApply(templateString string, replacement map[string]interface{}) string {
  22. t := fasttemplate.New(templateString, "{{", "}}")
  23. s := t.ExecuteString(replacement)
  24. return string(s)
  25. }
  26. func SendHTMLResponse(w http.ResponseWriter, msg string) {
  27. w.Header().Set("Content-Type", "text/html")
  28. w.Write([]byte(msg))
  29. }