计数在html怎么添加,在Go中显示html模板的计数

在我的html模板中:

{{range $index, $results := .}}

{{add $index 1}}{{.Title}}{{.Description}}

{{end}}

在执行代码中,我编写了一个传递给FuncMap的函数:

func add(x, y int) int {

return x + y

}

在我的处理程序中:

type ToDo struct {

Id          int

Title       string

Description string

}

func IndexHandler(writer http.ResponseWriter, request *http.Request) {

results := []ToDo{ToDo{5323, "foo", "bar"}, ToDo{632, "foo", "bar"}}

funcs := template.FuncMap{"add": add}

temp := template.Must(template.New("index.html").Funcs(funcs).ParseFiles(templateDir + "/index.html"))

temp.Execute(writer, results)

}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部