quicktemplate 高性能的golang模版引擎


quicktemplate 的設計與其他模版引擎的模式有點不一樣,而是直接將代碼邏輯嵌入到代碼中
同時也會編譯到二進制文件中(所以不支持on fly changing)

主要的幾個場景

  • 做為模版引擎(簡化代碼的編寫,不需要處理復雜的邏輯,對於內容的生成quicktemplate自動生成了,同時可以基於代碼的強類型能力調用)
  • 做為代碼的自動生成工具 (facebook的ent 對於orm 的處理就是基於此模式,高效而且很不錯)

參考使用

  • 項目結構
 
├── Makefile
├── README.md
├── demoapp
├── generate.go
├── go.mod
├── go.sum
├── main.go
└── templates
    ├── code.qtpl
    ├── code.qtpl.go
    ├── greetings.qtpl
    ├── greetings.qtpl.go
    ├── hello.qtpl
    ├── hello.qtpl.go
    ├── page.qtpl
    └── page.qtpl.go
  • 模版代碼
    page.qtpl
 
This is a base page template. All the other template pages implement this interface.
{% interface
Page {
   Title()
   Body()
}
%}
Page prints a page implementing Page interface.
{% func PageTemplate(p Page) %}
<html>
   <head>
      <title>{%= p.Title() %}</title>
   </head>
   <body>
      <h1>this is demo </h1>
      <div>
         <a href="/">return to main page</a>
      </div>
      {%= p.Body() %}
   </body>
</html>
{% endfunc %}
Base page implementation. Other pages may inherit from it if they need
overriding only certain Page methods
{% code type BasePage struct {
    Name string 
    Age int
} %}
{% func (p *BasePage) Title() %}
    {%code 
     p.Name = "dalongdemoaaaaaaa"
   %}
    <div>
        main title: {%s p.Name %}
    </div>
{% endfunc %}
{% func (p *BasePage) Body() %}
    <div>
        main body: {%d p.Age %}
    </div>
{% endfunc %}
  • main.go
    說明以上是一個基於fasthttp+quicktemplate 的簡單的web server.main 入口集成了fasthttp
 
package main
import (
  "bytes"
  "demoapp/templates"
  "github.com/valyala/fasthttp"
)
type login struct {
  Name string
  Age  int
}
func index(ctx *fasthttp.RequestCtx) {
  mypage := &templates.BasePage{
    Name: "dalong",
    Age:  33,
  }
  var buf bytes.Buffer
  templates.WritePageTemplate(&buf, mypage)
  ctx.SetContentType("text/html; charset=utf8")
  // Set arbitrary headers
  ctx.Response.Header.Set("X-My-Header", "my-header-value")
  ctx.Write(buf.Bytes())
}
func notFound(ctx *fasthttp.RequestCtx) {
  ctx.SetStatusCode(fasthttp.StatusNotFound)
  ctx.WriteString("not found ")
}
func (l *login) demo(ctx *fasthttp.RequestCtx) {
  switch string(ctx.Path()) {
  case "/":
    index(ctx)
  default:
    notFound(ctx)
  }
}
func main() {
  myHandler := &login{
    Name: "dalong",
    Age:  333,
  }
  fasthttp.ListenAndServe(":8080", myHandler.demo)
}
 
  • Makefile
all: generate
update:
  go get -u github.com/valyala/fasthttp
  go get -u github.com/valyala/quicktemplate/qtc
generate: update 
  go generate
vet: 
  go vet ./...

運行

  • 構建
make
  • 運行
go run main.go
  • 效果

 

 

說明

quicktemplate 在一些場景就不太合適了,比如需要動態更新的,當然如果我們的代碼具有完整的版本規划,而且變動比較少的,quicktemplate 是
一個很不錯的選擇

參考資料

https://github.com/benbjohnson/ego
https://github.com/sipin/gorazor
https://github.com/valyala/quicktemplate
https://www.makotemplates.org/
https://github.com/facebook/ent


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM