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
+12.1k Golang : convert(cast) string to integer value
+4.5k Java : Generate multiplication table example
+9.1k Golang : Handle sub domain with Gin
+18.4k Golang : Logging with logrus
+11.6k Swift : Convert (cast) Float to String
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+7.9k Javascript : Put image into Chrome browser's console
+10.9k Golang : Removes punctuation or defined delimiter from the user's input
+10.6k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+14.5k Golang : Rename directory
+19.3k Golang : Calculate entire request body length during run time