Standard library / text/template
text/template.Template.AddParseTree()
Associates a previously parsed syntax tree with a name. Do this before executing HTML templates. Expected stdout: 'Hello Ada\n'.
Formal Syntax & Structure
go syntaxgo
func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Name of the template. |
| tree | *parse.Tree | Parsed template syntax tree. |
Return Value
(*Template, error)
Associates a previously parsed syntax tree with a name.
Working Example
Example Snippetgo
package main
import ( "fmt" "os" "text/template" "text/template/parse")
func check(e error) { if e != nil { panic(e) }}
func main() { trees, e := parse.Parse("greet", "Hello {{.}}", "{{", "}}") check(e) t, e := template.New("greet").AddParseTree("greet", trees["greet"]) check(e) check(t.Execute(os.Stdout, "Ada")) fmt.Println()}