Golang html/template.Template.Lookup function examples
package html/template
Lookup returns the template with the given name(1st parameter) that is associated with t, or nil if there is no such template.
Golang html/template.Template.Lookup function usage examples
Example 1:
func parseHTMLTemplates(sets [][]string) error {
for _, set := range sets {
t := htmpl.New("")
t.Funcs(htmpl.FuncMap{
"urlDomain": urlDomain,
"urlTo": urlTo,
"itoa": strconv.Itoa,
"googleAnalyticsID": func() string { return os.Getenv("GOOGLE_ANALYTICS_ID") },
})
_, err := t.ParseFiles(joinTemplateDir(TemplateDir, set)...)
if err != nil {
return fmt.Errorf("template %v: %s", set, err)
}
t = t.Lookup("ROOT") // <-- here
if t == nil {
return fmt.Errorf("ROOT template not found in %v", set)
}
templates[set[0]] = t
}
return nil
}
Example 2:
func (r *RenderTemplateResult) Apply(req *http.Request, w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if r.TemplateSet.Lookup(r.TemplateName) == nil {
w.Write([]byte(fmt.Sprintf("can't found template:%s\n",
r.TemplateName)))
return
}
...
Reference :
Advertisement
Something interesting
Tutorials
+6.9k Golang : How to setup a disk space used monitoring service with Telegram bot
+14k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+11.9k Golang : Convert(cast) bigint to string
+5.6k Javascript : How to refresh page with JQuery ?
+7.9k Golang : Grayscale Image
+5k Google : Block or disable caching of your website content
+13.6k Golang : Set image canvas or background to transparent
+7.8k Golang : Getting Echo framework StartAutoTLS to work
+12.3k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+5.9k AWS S3 : Prevent Hotlinking policy
+10.3k Golang : Convert file content to Hex
+10k CodeIgniter : Load different view for mobile devices