Home Ask Login Register

Developers Planet

Your answer is one click away!

user3212497 February 2016

Render data into a template in golang

I want to render 2 different data(startup, event in this case) at the same time into html using golangs' html/template.

var startupData []model.StartupModel
err = startupCollection.Find(nil).Sort("-timestamp").All(&startupData )

var eventData []model.EventModel
err = eventCollection.Find(nil).Sort("-timestamp").All(&eventData )

How can I combine both startupData and EventData into a variable so that I can render as the following ?

    t.Execute(w, result) // result is eventData + startupData

Answers


Tinwor February 2016

You can create a struct and pass to Execute function

...
result := struct {
        StartupData []model.StartupModel
        EventData []model.EventModel
    }{
        StartupData : startupData,
        EventData : eventData,
    }
t.Execute(w, result)

Post Status

Asked in February 2016
Viewed 2,402 times
Voted 5
Answered 1 times

Search




Leave an answer


Quote of the day: live life