sách gpt4 ăn đã đi

go - 如何在 Go Gin 中使用模板获取动态内容

In lại 作者:IT王子 更新时间:2023-10-29 01:05:40 25 4
mua khóa gpt4 giày nike

我有一个简单的 Go/Gin 网络应用程序。我需要在 html 模板中放置一些动态内容。

例如我有几张表(数字是动态的),有几行(数字是动态的)。我需要将它们放在 html 模板中。有没有办法在代码中组合模板?我更愿意使用模板而不是在代码中构建表格。

我查看了一个教程 https://github.com/gin-gonic/gin但那里没有涵盖。

câu trả lời hay nhất

Bạn có thể sử dụng định nghĩa 来定义部分,使用 bản mẫu 来混合多个 HTML 部分。

gói chính

nhập khẩu (
"html/template"

"github.com/gin-gonic/gin"
)

đã từng là (
partial1 = `{{define "elm1"}}
element1
{{end}}`
partial2 = `{{define "elm2"}}
element2
{{end}}`
body = `{{template "elm1"}}{{template "elm2"}}`
)

hàm main() {
// Or use `ParseFiles` to parse tmpl files instead
t := template.Must(template.New("elements").Parse(body))

app := gin.Default()
app.GET("/", func(c *gin.Context) {
c.HTML(200, "elements", nil)
})
app.Run(":8000")
}

这是阅读的好地方 https://gohugo.io/templates/go-templates/

关于go - 如何在 Go Gin 中使用模板获取动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37813067/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress