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

posted on   荣锋亮  阅读(1247)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-12-03 polynote 安装试用
2018-12-03 openresty redis all in one docker demo
2018-12-03 hasura graphql-engine &&patroni docker-compose 环境运行
2018-12-03 使用patroni 解决hasura graphql-engine pg 数据库ha的问题
2018-12-03 使用patroni 构建高可用的pg 数据库
2018-12-03 zsh:no matches found 问题解决
2017-12-03 caddy quic 协议试用&& 几个问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示