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
+7.9k Golang Hello World Example
+5.6k Python : Print unicode escape characters and string
+7.1k Nginx : How to block user agent ?
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+16.4k Golang : How to implement two-factor authentication?
+5.1k Linux : How to set root password in Linux Mint
+15k Golang : How do I get the local IP (non-loopback) address ?
+7.3k Golang : How to convert strange string to JSON with json.MarshalIndent
+4.7k Chrome : How to block socketloop.com links in Google SERP?
+12.5k Golang : Forwarding a local port to a remote server example
+5.2k Golang : Calculate half life decay example
+10.9k Golang : Get UDP client IP address and differentiate clients by port number