V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
dzdh
V2EX  ›  Go 编程语言

有没有什么好用的 HTML 模板引擎

  •  
  •   dzdh · 8 天前 · 1540 次点击

    Go 自己的 html/template 有个问题

    完整项目中使用多个区块+layout 会导致永远只有有一个生效,或许是我使用姿势有问题

    举例说比如

    <!-- layout.html -->
    {{block "in"}{{end}}
    <!-- a.html -->
    {{template "layout.html"}}
    {{define "in"}}a{{end}}
    <!-- b.html -->
    {{template "layout.html"}}
    {{define "in"}}b{{end}}
    
    package main
    
    //go:embed *.html
    var fs embee.FS
    
    func main() {
        t := &template.New("").ParseFS(fs, "*.html")
        // 无论是 a.html 或者是 b.html 永远都是相同的内容
        // 因为所谓的 name 也算 define 内容一次被定义后会被后边的覆盖
        t.ExecuteTemplate(os.Stdout, "a.html", nil)
    }
    

    正确的方法应该是我这次需要显示 a.html ,就需要 template.new("").parsefs(fs,"layout.html","a.html")需要 b.html 的时候,再重新 parsefs(fs,"layout.html","b.html")

    感觉就很……

    要一个项目里几十上百个文件光是这个定义不得写死- -正确的使用姿势是什么呢?

    看到 go-pongo2 自己实现了一套 Django 的语法 {% extends "xx" %} 但是好像几年没更新了

    8 条回复    2024-06-21 09:04:45 +08:00
    wateryessence
        1
    wateryessence  
       8 天前 via iPhone
    htmx 挺火的
    qloog
        2
    qloog  
       8 天前
    我是用的 一个三方封装的一个: https://github.com/foolin/gin-template/tree/master
    使用方式:
    - 模板配置: https://github.com/go-eagle/eagle/blob/master/internal/routers/web.go
    - 模板示例: https://github.com/go-eagle/eagle/tree/master/internal/templates

    用起来感觉还好,没遇到过什么问题。
    Ayanokouji
        3
    Ayanokouji  
       8 天前   ❤️ 1
    参考这个项目,用 htmx
    https://github.com/mikestefanello/pagoda
    dzdh
        4
    dzdh  
    OP
       8 天前
    @qloog 看着是预先定义一个 master 作为 layout 。然后动态 parse 并且 parse 之前先 parse master 。只能是这种方法了吗。
    maladaxia
        5
    maladaxia  
       7 天前
    @Ayanokouji 非常感谢, 这个项目依赖的组件正是我喜欢的, bulma
    qloog
        6
    qloog  
       7 天前
    @dzdh 目前是这种,也是比较常见的一种方式。其他暂时没发现。
    cokyhe
        7
    cokyhe  
       7 天前
    dzdh
        8
    dzdh  
    OP
       7 天前
    @cokyhe 和 gintemplate 属于一类
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5388 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:52 · PVG 10:52 · LAX 19:52 · JFK 22:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.