dreadiscool
dreadiscool

Reputation: 1598

Golang/Revel template engine template with variable

Is there a way to pass a variable that was iterated over into a Golang/Revel template?

For example, in "header.html", I have

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

How can I use current index from the array as an argument to template? I tried embedding another {{.}} as shown in the Revel examples, but that leads to a template compliation error. Would the variable be something like $i?

For example, iterating through in Revel is done like so

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

However, I read that the . means nil.... how does this work in Revel?

Upvotes: 1

Views: 1195

Answers (1)

Herman Schaaf
Herman Schaaf

Reputation: 48475

If I understand your question correctly, you can use the range built-in to get the index, and then pass it to the template like this:

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

So if the templates variable was defined like this:

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

and Template.html contains:

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

Then the final output will be:

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

Upvotes: 2

Related Questions