golang mustache 模版引擎试用

主要是学习一个golang 的mustache模版引擎
cbroglie/mustache 是一个很不错的golang mustache 模版引擎,支持的功能还是比较多的,
以下是一个简单的使用

参考代码

  • go.mod
 
module demoapp
go 1.15
require (
    github.com/Jeffail/tunny v0.0.0-20181108205650-4921fff29480 // indirect
    github.com/cbroglie/mustache v1.2.0 // indirect
    github.com/stretchr/signature v0.0.0-20160104132143-168b2a1e1b56 // indirect
    github.com/stretchr/stew v0.0.0-20130812190256-80ef0842b48b // indirect
    github.com/stretchr/tracer v0.0.0-20140124184152-66d3696bba97 // indirect
)
  • main.go
package main
import (
    "log"
    "net/http"
    _ "net/http/pprof"
    "github.com/Jeffail/tunny"
    "github.com/cbroglie/mustache"
    "github.com/stretchr/stew/objects"
)
// default demo content
const (
    paylaod = `{
        "name": "Alice",
        "age":333,
        "users":[
            {
                "name":"dalong",
                "age":"333"
            }
        ]
    }`
)
func main() {
    pool := tunny.NewFunc(1000, func(payload interface{}) interface{} {
        log.Println(string(payload.([]byte)))
        jsonmap, err := objects.NewMapFromJSON(string(payload.([]byte)))
        if err != nil {
            log.Println(err)
            return nil
        }
        data, err := mustache.RenderFileInLayout("app.mustache", "layout.mustache", jsonmap.MSI())
        if err != nil {
            log.Println(err)
            return nil
        }
        log.Println(data)
        return data
    })
    defer pool.Close()
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        result := pool.Process([]byte(paylaod))
        if result != nil {
            msg := result.(string)
            w.Write([]byte(msg))
            return
        }
        w.Write([]byte("unknow"))
    })
    http.ListenAndServe(":8080", nil)
}
  • 默认
    app.mustache
 
<h1>{{name}}-{{age}}</h1>
<ul>
{{#users}}
<li>
-{{name}}
-{{age}}
</li>
{{/users}}
</ul>

layout.mustache

<html>
<head><title>Hi</title></head>
<body>
{{{content}}}
</body>
</html>
  • 简单说明
    以上是一个集成了模版的使用demo,因为需要json 数据,所以直接基于了一个工具类stretchr/stew 转换
    json string 为map,同时使用了Jeffail/tunny 以恶搞goroutine 包,集成了pprof 可以方便测试性能
  • 运行效果

 

 

参考资料

https://github.com/rongfengliang/mustache-tunny-learning
https://mustache.github.io/mustache.5.html
https://github.com/cbroglie/mustache
https://github.com/Jeffail/tunny

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

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-12-29 mjml nodejs 集成
2019-12-29 mjml强大&&灵活的邮件模版引擎
2018-12-29 Gravitational Teleport docker-compose组件独立部署运行
2017-12-29 postcss gulp 安装使用
2017-12-29 webpack 图片资源处理
2017-12-29 webpack libray 参考例子
2017-12-29 webpack css loader 使用

导航

< 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
点击右上角即可分享
微信分享提示