Search code examples
gogo-html-template

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

Solution

  • 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)