hertz
hertz copied to clipboard
Working with existing templating engine like hero / jade
a lot of them use this format, possible to give an example how to pass w http.ResponseWriter in hertz so we can just reuse existing templating engines?
http.HandleFunc("/users", func(w http.ResponseWriter, req *http.Request) {
var userList = []string {
"Alice",
"Bob",
"Tom",
}
// Had better use buffer pool. Hero exports `GetBuffer` and `PutBuffer` for this.
//
// For convenience, hero also supports `io.Writer`. For example, you can also define
// the function to `func UserList(userList []string, w io.Writer) (int, error)`,
// and then:
//
// template.UserList(userList, w)
//
buffer := new(bytes.Buffer)
template.UserList(userList, buffer)
w.Write(buffer.Bytes())
})
http.ListenAndServe(":8080", nil)
The implementation of hertz is not the same as the standard library.
If you are looking for a compatible way, check here: https://github.com/cloudwego/hertz/blob/develop/pkg/common/adaptor/request.go
Hi, @hiqsociety just want to let you know the community has added a working example of using Jade as template engine. Hope this example helps.