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

时间:2016-02-17 00:41:45

标签: go revel

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

例如,在“header.html”中,我有

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

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

例如,在Revel中迭代就像这样完成

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

然而,我读到了。意味着没有......这在Revel中如何运作?

1 个答案:

答案 0 :(得分:2)

如果我正确理解了您的问题,您可以使用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>
相关问题