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

10 天前
 dzdh

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" %} 但是好像几年没更新了

1581 次点击
所在节点    Go 编程语言
8 条回复
wateryessence
10 天前
htmx 挺火的
qloog
10 天前
我是用的 一个三方封装的一个: 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
10 天前
参考这个项目,用 htmx
https://github.com/mikestefanello/pagoda
dzdh
10 天前
@qloog 看着是预先定义一个 master 作为 layout 。然后动态 parse 并且 parse 之前先 parse master 。只能是这种方法了吗。
maladaxia
10 天前
@Ayanokouji 非常感谢, 这个项目依赖的组件正是我喜欢的, bulma
qloog
10 天前
@dzdh 目前是这种,也是比较常见的一种方式。其他暂时没发现。
cokyhe
10 天前
dzdh
9 天前
@cokyhe 和 gintemplate 属于一类

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1051045

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX