Golang html/template.Template.ParseFiles function examples
package html/template
ParseFiles parses the named files and associates the resulting templates with t. If an error occurs, parsing stops and the returned template is nil; otherwise it is t. There must be at least one file.
Golang html/template.Template.ParseFiles function usage examples
Example 1:
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
Example 2:
var templates = template.Must(template.ParseFiles(
"header.html",
"index.html",
"apps/types.html",
"music.html", "tmpl/snips.tmpl",
"footer.html"))
References :
Advertisement
Something interesting
Tutorials
+51.4k Golang : Check if item is in slice/array
+5.4k Golang : Reclaim memory occupied by make() example
+9.8k Golang : Resumable upload to Google Drive(RESTful) example
+10.6k Golang : Flip coin example
+5.3k Swift : Convert string array to array example
+15k Golang : How do I get the local IP (non-loopback) address ?
+20.2k Golang : How to get struct tag and use field name to retrieve data?
+15.2k Golang : How to check if IP address is in range
+7.7k Golang : Command line ticker to show work in progress
+40.1k Golang : UDP client server read write example
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)