Golang html/template.ParseFiles function examples
package html/template
ParseFiles creates a new Template and parses the template definitions from the named files. The returned template's name will have the (base) name and (parsed) contents of the first file. There must be at least one file. If an error occurs, parsing stops and the returned *Template is nil.
Golang html/template.ParseFiles function usage examples
Example 1:
var templates = template.Must(template.ParseFiles(
"header.html",
"index.html",
"apps/types.html",
"music.html", "tmpl/snips.tmpl",
"footer.html"))
Example 2:
templateDir := sp.config.templateDir // var templateDir string
tmpl, err := template.ParseFiles(templateDir+"/main.tpl", templateDir+"/body_"+sp.error.computedStatus+".tpl")
if err != nil {
http.Error(w, "Unable to serve page : "+sp.error.computedStatus, code)
return
}
Example 3:
package main
import (
"fmt"
"html/template"
"os"
)
func main() {
t := template.New("HELLO")
var templates = template.Must(t.ParseFiles("defaultTemplate.html"))
data := map[string]interface{}{
"Title": "Hello World!",
}
templates.ExecuteTemplate(os.Stdout, "defaultTemplate.html", data)
fmt.Println("\n\n")
fmt.Println("Template name is : ", t.Name())
}
content of defaultTemplate.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ .Title }}</title>
</head>
Output :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World!</title>
</head>
Template name is : HELLO
References :
http://golang.org/pkg/html/template/#ParseFiles
https://github.com/aclissold/andrewclissold.appspot.com/blob/master/app.go
Advertisement
Something interesting
Tutorials
+11.9k Golang : How to parse plain email text and process email header?
+12.8k Golang : Convert int(year) to time.Time type
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+9.3k Golang : How to get garbage collection data?
+10.3k Golang : Convert file content to Hex
+5.3k PHP : Hide PHP version information from curl
+11.6k Android Studio : Create custom icons for your application example
+13.9k Golang : How to check if a file is hidden?
+30.8k Golang : Download file example
+19.2k Golang : Delete item from slice based on index/key position
+10k Golang : Get escape characters \u form from unicode characters
+4.7k Linux/MacOSX : How to symlink a file?