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
+4.8k PHP : Extract part of a string starting from the middle
+6.3k Golang : Selection sort example
+26.4k Golang : Convert(cast) string to uint8 type and back to string
+25.8k Golang : Daemonizing a simple web server process example
+12.7k Golang : Pass database connection to function called from another package and HTTP Handler
+5.9k Golang : Extract unicode string from another unicode string example
+3.6k Java : Get FX sentiment from website example
+8.3k Swift : Convert (cast) Character to Integer?
+7.6k Javascript : Push notifications to browser with Push.js
+9k Golang : Capture text return from exec function example
+15.2k Golang : How to check if IP address is in range
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example