mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
26
_examples/view/herotemplate/README.md
Normal file
26
_examples/view/herotemplate/README.md
Normal file
@@ -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
|
||||
54
_examples/view/herotemplate/app.go
Normal file
54
_examples/view/herotemplate/app.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/kataras/iris/v12/_examples/response-writer/herotemplate/template"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// $ 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) {
|
||||
ctx.Gzip(true)
|
||||
ctx.ContentType("text/html")
|
||||
|
||||
userList := []string{
|
||||
"Alice",
|
||||
"Bob",
|
||||
"Tom",
|
||||
}
|
||||
|
||||
// Had better use buffer sync.Pool.
|
||||
// Hero(github.com/shiyanhui/hero/hero) exports GetBuffer and PutBuffer for this.
|
||||
//
|
||||
// buffer := hero.GetBuffer()
|
||||
// defer hero.PutBuffer(buffer)
|
||||
// buffer := new(bytes.Buffer)
|
||||
// template.UserList(userList, buffer)
|
||||
// ctx.Write(buffer.Bytes())
|
||||
|
||||
// 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 standard http.ResponseWriter
|
||||
// but still 100% compatible, GzipResponseWriter too:
|
||||
// _, err := template.UserListToWriter(userList, ctx.GzipResponseWriter())
|
||||
buffer := new(bytes.Buffer)
|
||||
template.UserList(userList, buffer)
|
||||
|
||||
_, err := ctx.Write(buffer.Bytes())
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
11
_examples/view/herotemplate/template/index.html
Normal file
11
_examples/view/herotemplate/template/index.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<%@ body { %>
|
||||
<% } %>
|
||||
</body>
|
||||
</html>
|
||||
3
_examples/view/herotemplate/template/index.html.go
Normal file
3
_examples/view/herotemplate/template/index.html.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Code generated by hero.
|
||||
// DO NOT EDIT!
|
||||
package template
|
||||
3
_examples/view/herotemplate/template/user.html
Normal file
3
_examples/view/herotemplate/template/user.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<li>
|
||||
<%= user %>
|
||||
</li>
|
||||
3
_examples/view/herotemplate/template/user.html.go
Normal file
3
_examples/view/herotemplate/template/user.html.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Code generated by hero.
|
||||
// DO NOT EDIT!
|
||||
package template
|
||||
11
_examples/view/herotemplate/template/userlist.html
Normal file
11
_examples/view/herotemplate/template/userlist.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<%: func UserList(userList []string, buffer *bytes.Buffer) %>
|
||||
|
||||
<%~ "index.html" %>
|
||||
|
||||
<%@ body { %>
|
||||
<% for _, user := range userList { %>
|
||||
<ul>
|
||||
<%+ "user.html" %>
|
||||
</ul>
|
||||
<% } %>
|
||||
<% } %>
|
||||
40
_examples/view/herotemplate/template/userlist.html.go
Normal file
40
_examples/view/herotemplate/template/userlist.html.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Code generated by hero.
|
||||
// DO NOT EDIT!
|
||||
package template
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/shiyanhui/hero"
|
||||
)
|
||||
|
||||
func UserList(userList []string, buffer *bytes.Buffer) {
|
||||
buffer.WriteString(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
`)
|
||||
for _, user := range userList {
|
||||
buffer.WriteString(`
|
||||
<ul>
|
||||
`)
|
||||
buffer.WriteString(`<li>
|
||||
`)
|
||||
hero.EscapeHTML(user, buffer)
|
||||
buffer.WriteString(`
|
||||
</li>
|
||||
`)
|
||||
|
||||
buffer.WriteString(`
|
||||
</ul>
|
||||
`)
|
||||
}
|
||||
|
||||
buffer.WriteString(`
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
11
_examples/view/herotemplate/template/userlistwriter.html
Normal file
11
_examples/view/herotemplate/template/userlistwriter.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<%: func UserListToWriter(userList []string, w io.Writer) (int, error)%>
|
||||
|
||||
<%~ "index.html" %>
|
||||
|
||||
<%@ body { %>
|
||||
<% for _, user := range userList { %>
|
||||
<ul>
|
||||
<%+ "user.html" %>
|
||||
</ul>
|
||||
<% } %>
|
||||
<% } %>
|
||||
43
_examples/view/herotemplate/template/userlistwriter.html.go
Normal file
43
_examples/view/herotemplate/template/userlistwriter.html.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
`)
|
||||
for _, user := range userList {
|
||||
_buffer.WriteString(`
|
||||
<ul>
|
||||
`)
|
||||
_buffer.WriteString(`<li>
|
||||
`)
|
||||
hero.EscapeHTML(user, _buffer)
|
||||
_buffer.WriteString(`
|
||||
</li>
|
||||
`)
|
||||
|
||||
_buffer.WriteString(`
|
||||
</ul>
|
||||
`)
|
||||
}
|
||||
|
||||
_buffer.WriteString(`
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
return w.Write(_buffer.Bytes())
|
||||
}
|
||||
Reference in New Issue
Block a user