Golang/Revel 带有变量的模板引擎模板

Golang/Revel template engine template with variable

有没有办法将迭代过的变量传递到 Golang/Revel 模板中?

例如,在"header.html"中,我有

{{range .templates}}
    {{template "something" .}}
{{end}}

如何使用数组中的当前索引作为模板的参数?我尝试嵌入另一个 {{.}},如 Revel 示例所示,但这会导致模板编译错误。变量会是 $i 吗?

例如,Revel 中的迭代就是这样完成的

{{range .messages}}
    <p>{{.}}</p>
{{end}}

但是,我读到 .表示无....这在 Revel 中如何工作?

如果我对你的问题理解正确,你可以使用内置的 range 来获取索引,然后像这样将其传递给模板:

{{range $i, $t := .templates}}
   {{template "Template.html" $i}}
{{end}}

因此,如果 templates 变量定义如下:

templates := []string{"One", "Two"}

Template.html包含:

This is from Template.html: {{ . }}<br>

那么最终输出将是:

This is from Template.html: 0<br>
This is from Template.html: 1<br>