hertz icon indicating copy to clipboard operation
hertz copied to clipboard

Working with existing templating engine like hero / jade

Open hiqsociety opened this issue 3 years ago • 2 comments

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)

hiqsociety avatar Jul 06 '22 02:07 hiqsociety

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

welkeyever avatar Jul 06 '22 06:07 welkeyever

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.

Haswf avatar Aug 13 '22 06:08 Haswf