Golang html/template.Template.ParseGlob function example

package html/template

ParseGlob parses the template definitions in the files identified by the pattern(1st parameter - a.k.a wild card) and associates the resulting templates with t. The pattern is processed by filepath.Glob and must match at least one file. ParseGlob is equivalent to calling t.ParseFiles with the list of files matched by the pattern.

Golang html/template.Template.ParseGlob function usage example

 package main

 import (
 "fmt"
 "html/template"
 "os"
 )

 func main() {

 t := template.New("HELLO")

 var templates = template.Must(t.ParseGlob("*.html")) // we look for defaultTemplate.html file

 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 :

https://www.socketloop.com/references/golang-html-template-parseglob-function-examples

http://golang.org/pkg/html/template/#Template.ParseGlob

  See also : Golang html/template.ParseGlob function examples

Advertisement