Hero

    • 高性能。
    • 使用方便。
    • 强大。 模板扩展和包含支持。
    • 文件更改时自动编译。
    1. package main
    2. import (
    3. "bytes"
    4. "./template"
    5. "github.com/kataras/iris"
    6. )
    7. // $ go get -u github.com/shiyanhui/hero/hero
    8. // $ go run main.go
    9. // 了解更多 https://github.com/shiyanhui/hero
    10. func main() {
    11. app := iris.New()
    12. app.Get("/users", func(ctx iris.Context) {
    13. ctx.ContentType("text/html")
    14. var userList = []string{
    15. "Alice",
    16. "Tom",
    17. }
    18. //最好使用buffer sync.Pool
    19. //Hero(github.com/shiyanhui/hero)为此导出了GetBuffer和PutBuffer
    20. // buffer := hero.GetBuffer()
    21. // defer hero.PutBuffer(buffer)
    22. // buffer := new(bytes.Buffer)
    23. // template.UserList(userList, buffer)
    24. // ctx.Write(buffer.Bytes())
    25. //使用io.Writer进行自动缓冲管理(hero built-in 缓冲池),
    26. // iris context通过其ResponseWriter实现io.Writer
    27. //这是标准http.ResponseWriter的增强版本
    28. //但仍然100%兼容,GzipResponseWriter也同样兼容:
    29. // _, err := template.UserListToWriter(userList, ctx.GzipResponseWriter())
    30. buffer := new(bytes.Buffer)
    31. _, err := ctx.Write(buffer.Bytes())
    32. if err != nil {
    33. ctx.StatusCode(iris.StatusInternalServerError)
    34. }
    35. })
    36. app.Run(iris.Addr(":8080"))
    37. }
    1. <li>
    2. <%= user %>
    3. </li>
    1. <%: func UserListToWriter(userList []string, w io.Writer) (int, error)%>
    2. <%~ "index.html" %>
    3. <%@ body { %>
    4. <% for _, user := range userList { %>
    5. <ul>
    6. <%+ "user.html" %>
    7. </ul>
    8. <% } %>
    9. <% } %>
    • 引入相应的包,建好所有所有文件
    • 传送们