I'm doing my first project in Golang, and I'm trying to create a form with two lists to choose options from, as following:
webpage with a form, it has two lists to choose options from and a text field
However, If I want to make those lists to retrieve their entries from two tables from a SQLite database, the module "templates" only allows me to send one query per template, something like
func Person(w http.ResponseWriter, r *http.Request) {
db := ConnectDB()
arrP := GetPerson(db)
templates.ExecuteTemplate(w, "person", arrP)
}
Thus, only the first list will pull up information from the range (in this particular case, it'll display valid options from the 'person' table, and the second list will keep showing the entries I hard coded myself.
Thanks @Zombo for replying. To clarify some things, the function I posted opens up the connection with the database
db := ConnectDB()
Then it creates arrP (array of 'person' type) and calls the built-in templates.ExecuteTemplate()
templates.ExecuteTemplate(w, "person", arrP)
Which will render the "person" html template. However, in that same page I'm trying to put another selection form to choose 'Service'. If I try something like
func Person(w http.ResponseWriter, r *http.Request) {
db := ConnectDB()
arrP := GetPerson(db)
arrS := GetService(db)
templates.ExecuteTemplate(w, "person", arrP, arrS)
}
It will complain because I'm passing more arguments than expected. Therefore, only the list of persons will render properly with the information from the database; however, the list of services doesn't have any source to pull its entries from.
CodePudding user response:
@Zombo gave a neat answer, using a map we can pass multiple arrays to a template to render several list / tables.
map[string]any{“arrP”: arrP, “arrS”: arrS}
And then use $.arrS in the .tmpl file.