[Golang] Example for block Action in Template package


Example for block action in Golang text/template and html/template packages.

Run Code on Go Playground

package main

import (
        "os"
        "text/template"
)

var layout = `
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>{{block "title" .}}{{end}}</title>
</head>
<body>
</body>
</html>
`

var index = `
{{define "title"}}My Site Name{{end}}
`

func main() {
        layoutTmpl, _ := template.New("layout").Parse(layout)
        indexTmpl, _ := layoutTmpl.Parse(index)
        indexTmpl.Execute(os.Stdout, nil)
}

Tested on: Ubuntu Linux 15.10, Go 1.6.


References:

[1]Go 1.6 is released - The Go Blog
[2]new template example program
[3]{{block}} action
[4]Templates - Go 1.6 Release Notes - The Go Programming Language
[5]

Template inheritance ? : golang

How to Use Template Blocks in Go 1.6 - Improving Efficiency with Technology | Joseph Spurrier

Including html/template snippets: is there a better way? : golang