FAQ
Hello,

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.

Search Discussions

  • Martin Schnabel at Apr 13, 2015 at 7:18 pm

    On 04/13/2015 08:32 PM, [email protected] wrote:
    Hello,

    I am trying to understand how I can use templates to find my pages from
    nested folders.
    you can use {{ define "anytemplatename" }} template content {{ end }} in
    each file to name the templates explicitly.

    --
    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.
  • Maria at Apr 13, 2015 at 7:38 pm
    thanks Mb0,
    I understand that concept, I was hoping to learn if I can use the folder
    structure and have the function render it.
    On Monday, April 13, 2015 at 3:19:14 PM UTC-4, mb0 wrote:
    On 04/13/2015 08:32 PM, [email protected] <javascript:> wrote:
    Hello,

    I am trying to understand how I can use templates to find my pages from
    nested folders.
    you can use {{ define "anytemplatename" }} template content {{ end }} in
    each file to name the templates explicitly.
    --
    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.
  • Lars Tørnes Hansen at Apr 13, 2015 at 8:36 pm
    If i understand you correctly - the problem is that a user folder is only
    known at runtime?

    You could use some function to 1) search for a user folder(s) and 2) create
    a list filename for each user folder. Next 3) filter files, so you only
    have tempalte files 4) read the template as a string in each file, and then
    render each string (template file).

    /Lars

    Den mandag den 13. april 2015 kl. 21.38.23 UTC+2 skrev Maria:
    thanks Mb0,
    I understand that concept, I was hoping to learn if I can use the folder
    structure and have the function render it.
    On Monday, April 13, 2015 at 3:19:14 PM UTC-4, mb0 wrote:
    On 04/13/2015 08:32 PM, [email protected] wrote:
    Hello,

    I am trying to understand how I can use templates to find my pages from
    nested folders.
    you can use {{ define "anytemplatename" }} template content {{ end }} in
    each file to name the templates explicitly.
    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 13, '15 at 6:36p
activeApr 13, '15 at 8:36p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase