Search code examples
htmlgorevel

Revel template multiple variables, access one inside the others range


I have a controller passing two variables

func (a App) Page() revel.Result {
    var g []*G
    ...
    return c.Render(p, g)
}

in my .html I want to iterate over g. Is it possible to access p inside the iteration? I couldnt manage. My try looks like the following

{{range .g}}
... // print g related stuff
.p
{{end}}

and it throws can't evaluate field p in type *G.


Solution

  • Revel seems to be using Go's template engine, therefore I guess you should be able to use whatever stuff's allowed with html/template like variables.

    {{$p := .p}}
    {{range .g}}
    ... // print g related stuff
    {{$p}}
    {{end}}
    

    See documentation for more info https://golang.org/pkg/text/template/#hdr-Variables

    Please note, if p is not a key in a map but a field of a struct, you won't be able to access it, i believe, since it is not exported. Ok I understand now, Revel seems to derive the names inside the template from the names of the arguments that were passed to Render.