pug.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package view
  2. import (
  3. "bytes"
  4. "os"
  5. "github.com/Joker/jade"
  6. )
  7. // Pug (or Jade) returns a new pug view engine.
  8. // It shares the same exactly logic with the
  9. // html view engine, it uses the same exactly configuration.
  10. // The given "extension" MUST begin with a dot.
  11. //
  12. // Read more about the Jade Go Parser: https://github.com/Joker/jade
  13. //
  14. // Usage:
  15. // Pug("./views", ".pug") or
  16. // Pug(iris.Dir("./views"), ".pug") or
  17. // Pug(embed.FS, ".pug") or Pug(AssetFile(), ".pug") for embedded data.
  18. //
  19. // Examples:
  20. // https://github.com/kataras/iris/tree/main/_examples/view/template_pug_0
  21. // https://github.com/kataras/iris/tree/main/_examples/view/template_pug_1
  22. // https://github.com/kataras/iris/tree/main/_examples/view/template_pug_2
  23. // https://github.com/kataras/iris/tree/main/_examples/view/template_pug_3
  24. func Pug(fs interface{}, extension string) *HTMLEngine {
  25. s := HTML(fs, extension)
  26. s.name = "Pug"
  27. s.middleware = func(name string, text []byte) (contents string, err error) {
  28. jade.ReadFunc = func(filename string) ([]byte, error) {
  29. return asset(s.fs, filename)
  30. }
  31. tmpl := jade.New(name)
  32. exec, err := tmpl.Parse(text)
  33. if err != nil {
  34. return
  35. }
  36. b := new(bytes.Buffer)
  37. exec.WriteIn(b)
  38. jade.ReadFunc = os.ReadFile // reset to original.
  39. return b.String(), nil
  40. }
  41. return s
  42. }