I tried to add a simple modificaiton to the template example to go over a list and render the template but the standard .tmpl for loop or {range} doesnt work, any example for list render in the template?
here is my code
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "Main website",
"author": "Medya",
"movies": []string{"a","b","c"},
})
})
router.Run(":8080")
}
and here is my template.
<html>
<h1>
{{ .title }}
</h1>
<body>
{{ .author}}
{% for .m in movies %}
{{ .m }}
{% endfor %}
</body>
</html>
Comment From: medyagh
update I fixed it by
{{ range .movies }}
{{ . }}
{{ end }}
sorry for the noise. just a note, the syntax doesnt match django's .tmpl template (there you do one curly brace for for loop)
Comment From: jimmyyem
update I fixed it by
{{ range .movies }} {{ . }} {{ end }}
sorry for the noise. just a note, the syntax doesnt match django's .tmpl template (there you do one curly brace for for loop)
that usage works for me.
Comment From: benstigsen
I know that the question has been answered. But it felt appropriate to ask in here. How do I do a for loop, while also keeping the index?