Re: [go-nuts] template Funcs with ParseFile and Execute
Sry for little info, template.Base is a constant ("templates/base.html", a path) and the same for templateName passed from my controller. But what do i pass in template.New() ? On Saturday, March 25, 2017 at 10:17:23 PM UTC+5:30, Yves Junqueira wrote: > > Your ParseFiles call has a "templateName" argument in it. The ParseFiles > function only takes file paths, not template names. > > And templates.New takes a template name, not a file path. > > Hope this helps. > > On Sat, Mar 25, 2017 at 9:14 AM, priyank pulumati <pulumati...@gmail.com > > wrote: > >> hello, >> im building a go web app and facing an issue with Executing templates >> with custom functions >> >> func CustomTemplateExecute(res http.ResponseWriter, req *http.Request, >> templateName string, data map[string]interface{}) { >> // Append common templates and data structs and execute template >> var funcMap = template.FuncMap{ >> "humanSize": humanSize, >> } >> t, _ := template.New("").Funcs(funcMap).ParseFiles(templates.BASE, >> templates.NOTIFICATIONS, templates.TICKER, templateName) >> if len(data) == 0 { >> data = make(map[string]interface{}) >> data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) >> data["isadmin"] = store.IsAdmin(context.Get(req, "userid").(string)) >> } else { >> data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) >> data["isadmin"] = store.IsAdmin(context.Get(req, "userid").(string)) >> } >> t.Execute(res, data) >> } >> >> func humanSize() string { >> return "Hello" >> } >> >> I get template is empty error, i have tried giving a name to template.new >> and its the same error. >> Any help is appreciated, thank you ! >> >> -- >> 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 golang-nuts...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] template Funcs with ParseFile and Execute
Sry for little information, templateName is passed from my controllers with a constant (like "templates/home.html"), so there is no problem with that. In template.new(), what do i pass ? On Saturday, March 25, 2017 at 10:17:23 PM UTC+5:30, Yves Junqueira wrote: > > Your ParseFiles call has a "templateName" argument in it. The ParseFiles > function only takes file paths, not template names. > > And templates.New takes a template name, not a file path. > > Hope this helps. > > On Sat, Mar 25, 2017 at 9:14 AM, priyank pulumati <pulumati...@gmail.com > > wrote: > >> hello, >> im building a go web app and facing an issue with Executing templates >> with custom functions >> >> func CustomTemplateExecute(res http.ResponseWriter, req *http.Request, >> templateName string, data map[string]interface{}) { >> // Append common templates and data structs and execute template >> var funcMap = template.FuncMap{ >> "humanSize": humanSize, >> } >> t, _ := template.New("").Funcs(funcMap).ParseFiles(templates.BASE, >> templates.NOTIFICATIONS, templates.TICKER, templateName) >> if len(data) == 0 { >> data = make(map[string]interface{}) >> data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) >> data["isadmin"] = store.IsAdmin(context.Get(req, "userid").(string)) >> } else { >> data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) >> data["isadmin"] = store.IsAdmin(context.Get(req, "userid").(string)) >> } >> t.Execute(res, data) >> } >> >> func humanSize() string { >> return "Hello" >> } >> >> I get template is empty error, i have tried giving a name to template.new >> and its the same error. >> Any help is appreciated, thank you ! >> >> -- >> 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 golang-nuts...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: template Funcs with ParseFile and Execute
Error when i say template.New(templates.BASE) template: templates/base.html: "templates/base.html" is an incomplete or empty template -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] template Funcs with ParseFile and Execute
hello, im building a go web app and facing an issue with Executing templates with custom functions func CustomTemplateExecute(res http.ResponseWriter, req *http.Request, templateName string, data map[string]interface{}) { // Append common templates and data structs and execute template var funcMap = template.FuncMap{ "humanSize": humanSize, } t, _ := template.New("").Funcs(funcMap).ParseFiles(templates.BASE, templates.NOTIFICATIONS, templates.TICKER, templateName) if len(data) == 0 { data = make(map[string]interface{}) data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) data["isadmin"] = store.IsAdmin(context.Get(req, "userid").(string)) } else { data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) data["isadmin"] = store.IsAdmin(context.Get(req, "userid").(string)) } t.Execute(res, data) } func humanSize() string { return "Hello" } I get template is empty error, i have tried giving a name to template.new and its the same error. Any help is appreciated, thank you ! -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Map value assignment inside if loop
Hello, Go newbie here func (data map[string] interface {}) { if len(data) == 0 { data := make(map[string]interface{}) data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) } else { data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) } log.Println(data["user"]) } This produces in console, where as, func (data map[string] interface {}) { if len(data) == 0 { data := make(map[string]interface{}) data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) } else { data["user"], _ = store.GetUser(context.Get(req, "userid").(string)) log.Println(data["user"]) } } This produces the right value, why is it so ? -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.