I am trying to understand how I can use templates to find my pages from
nested folders.
I have a
Templates folder - and inside is
Admin folder - inside are
list.html
edit.html
create.html
and I want to add to the Templates folder
Users folder
list.html
edit.html
create.html
I can make the Admin folder work but if I add a Users folder, my app breaks
because in the func for the route I can only use one edit file.
renderTemplate(rw, "edit", c)
If I try something like renderTemplate(rw, "users/edit", c)
It will not work.
Is there something I can do, I have looked
at https://github.com/golang-samples/template and go docs but I can't seem
to find a way to this. Below is some of my code:
var templates = template.Must(template.ParseFiles(
"templates/admin/list.html",
"templates/admin/create.html",
"templates/admin/edit.html",
// I want to change the ones below to users/list.html etc..
"templates/user-list.html",
"templates/user-edit.html",
"templates/user-create.html",
))
func renderTemplate(w http.ResponseWriter, tmpl string, c interface{}) {
err := templates.ExecuteTemplate(w, tmpl+".html", c)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (c *Context) AdminEdit(rw web.ResponseWriter, req *web.Request) {
c.Title = "Admin Edit"
c.Body = "Edit or Update a new admin."
renderTemplate(rw, "edit", c)
}
func (c *Context) UserEdit(rw web.ResponseWriter, req *web.Request) {
c.Title = "Individual User Page"
c.Body = "Super Admins only. "
renderTemplate(rw, "edit", c) // Here is the problem Is there a way I
can use "users/edit" somehow?
}
//my routes
rootRouter.Get("/", (*Context).Home)
rootRouter.Get("/dash", (*Context).Dashboard)
rootRouter.Get("/list", (*Context).AdminList)
rootRouter.Get("/admin/create", (*Context).AdminCreate)
rootRouter.Get("/admin/edit", (*Context).AdminEdit)
rootRouter.Get("/user-list", (*Context).UserList)
rootRouter.Get("/users-edit", (*Context).UserEdit)
etc..
Any help is appreciated.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.