diff --git a/_examples/README.md b/_examples/README.md index 1f49a85d..83272b5f 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -259,7 +259,7 @@ convert any custom type into a response dispatcher by implementing the `mvc.Resu - [Embedding Templates Into App Executable File](view/embedding-templates-into-app/main.go) -You can serve [quicktemplate](https://github.com/valyala/quicktemplate) files too, simply by using the `context#ResponseWriter`, take a look at the [http_responsewriter/quicktemplate](http_responsewriter/quicktemplate) example. +You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [hero](https://github.com/shiyanhui/hero/hero) files too, simply by using the `context#ResponseWriter`, take a look at the [http_responsewriter/quicktemplate](http_responsewriter/quicktemplate) and [http_responsewriter/hero] examples. ### Authentication @@ -290,6 +290,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) files to ### How to Write to `context.ResponseWriter() http.ResponseWriter` - [Write `valyala/quicktemplate` templates](http_responsewriter/quicktemplate) +- [Write `shiyanhui/hero` templates](http_responsewriter/hero) - [Text, Markdown, HTML, JSON, JSONP, XML, Binary](http_responsewriter/write-rest/main.go) - [Stream Writer](http_responsewriter/stream-writer/main.go) - [Transactions](http_responsewriter/transactions/main.go) diff --git a/_examples/http_responsewriter/hero/README.md b/_examples/http_responsewriter/hero/README.md new file mode 100644 index 00000000..ff71e067 --- /dev/null +++ b/_examples/http_responsewriter/hero/README.md @@ -0,0 +1,26 @@ +# Hero Template Example + +This folder contains the iris version of the original hero's example: https://github.com/shiyanhui/hero/tree/master/examples/app. + +Iris is 100% compatible with `net/http` so you don't have to change anything else +except the handler input from the original example. + +The only inline handler's changes were: + +From: + +```go +if _, err := w.Write(buffer.Bytes()); err != nil { +// and +template.UserListToWriter(userList, w) +``` +To: +```go +if _, err := ctx.Write(buffer.Bytes()); err != nil { +// and +template.UserListToWriter(userList, ctx) +``` + +So easy. + +Read more at: https://github.com/shiyanhui/hero \ No newline at end of file diff --git a/_examples/http_responsewriter/hero/app.go b/_examples/http_responsewriter/hero/app.go new file mode 100644 index 00000000..f25bb9e1 --- /dev/null +++ b/_examples/http_responsewriter/hero/app.go @@ -0,0 +1,55 @@ +package main + +import ( + "bytes" + "log" + + "github.com/kataras/iris/_examples/http_responsewriter/hero/template" + + "github.com/kataras/iris" +) + +// $ go get -u github.com/shiyanhui/hero/hero +// $ go run app.go +// +// Read more at https://github.com/shiyanhui/hero/hero +func main() { + + app := iris.New() + + app.Get("/users", func(ctx iris.Context) { + var userList = []string{ + "Alice", + "Bob", + "Tom", + } + + // Had better use buffer sync.Pool. + // Hero exports GetBuffer and PutBuffer for this. + // + // buffer := hero.GetBuffer() + // defer hero.PutBuffer(buffer) + buffer := new(bytes.Buffer) + template.UserList(userList, buffer) + + if _, err := ctx.Write(buffer.Bytes()); err != nil { + log.Printf("ERR: %s\n", err) + } + }) + + app.Get("/users2", func(ctx iris.Context) { + var userList = []string{ + "Alice", + "Bob", + "Tom", + } + + // using an io.Writer for automatic buffer management (i.e. hero built-in buffer pool), + // iris context implements the io.Writer by its ResponseWriter + // which is an enhanced version of the standar http.ResponseWriter + // but still 100% compatible. + template.UserListToWriter(userList, ctx) + }) + + app.Run(iris.Addr(":8080")) +} diff --git a/_examples/http_responsewriter/hero/template/index.html b/_examples/http_responsewriter/hero/template/index.html new file mode 100644 index 00000000..c39ba0e7 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/index.html @@ -0,0 +1,11 @@ + + + + + + + + <%@ body { %> + <% } %> + + diff --git a/_examples/http_responsewriter/hero/template/index.html.go b/_examples/http_responsewriter/hero/template/index.html.go new file mode 100644 index 00000000..da8b5842 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/index.html.go @@ -0,0 +1,3 @@ +// Code generated by hero. +// DO NOT EDIT! +package template diff --git a/_examples/http_responsewriter/hero/template/user.html b/_examples/http_responsewriter/hero/template/user.html new file mode 100644 index 00000000..812fd4c5 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/user.html @@ -0,0 +1,3 @@ +
  • + <%= user %> +
  • diff --git a/_examples/http_responsewriter/hero/template/user.html.go b/_examples/http_responsewriter/hero/template/user.html.go new file mode 100644 index 00000000..da8b5842 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/user.html.go @@ -0,0 +1,3 @@ +// Code generated by hero. +// DO NOT EDIT! +package template diff --git a/_examples/http_responsewriter/hero/template/userlist.html b/_examples/http_responsewriter/hero/template/userlist.html new file mode 100644 index 00000000..afb27d71 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/userlist.html @@ -0,0 +1,11 @@ +<%: func UserList(userList []string, buffer *bytes.Buffer) %> + +<%~ "index.html" %> + +<%@ body { %> + <% for _, user := range userList { %> + + <% } %> +<% } %> diff --git a/_examples/http_responsewriter/hero/template/userlist.html.go b/_examples/http_responsewriter/hero/template/userlist.html.go new file mode 100644 index 00000000..ebf6fcf1 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/userlist.html.go @@ -0,0 +1,41 @@ +// Code generated by hero. +// DO NOT EDIT! +package template + +import ( + "bytes" + + "github.com/shiyanhui/hero" +) + +func UserList(userList []string, buffer *bytes.Buffer) { + buffer.WriteString(` + + + + + + + `) + for _, user := range userList { + buffer.WriteString(` + + `) + } + + buffer.WriteString(` + + +`) + +} diff --git a/_examples/http_responsewriter/hero/template/userlistwriter.html b/_examples/http_responsewriter/hero/template/userlistwriter.html new file mode 100644 index 00000000..59e68609 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/userlistwriter.html @@ -0,0 +1,11 @@ +<%: func UserListToWriter(userList []string, w io.Writer) (int, error)%> + +<%~ "index.html" %> + +<%@ body { %> + <% for _, user := range userList { %> + + <% } %> +<% } %> diff --git a/_examples/http_responsewriter/hero/template/userlistwriter.html.go b/_examples/http_responsewriter/hero/template/userlistwriter.html.go new file mode 100644 index 00000000..114bffa9 --- /dev/null +++ b/_examples/http_responsewriter/hero/template/userlistwriter.html.go @@ -0,0 +1,44 @@ +// Code generated by hero. +// DO NOT EDIT! +package template + +import ( + "io" + + "github.com/shiyanhui/hero" +) + +func UserListToWriter(userList []string, w io.Writer) (int, error) { + _buffer := hero.GetBuffer() + defer hero.PutBuffer(_buffer) + _buffer.WriteString(` + + + + + + + `) + for _, user := range userList { + _buffer.WriteString(` + + `) + } + + _buffer.WriteString(` + + +`) + return w.Write(_buffer.Bytes()) + +}