add chapter 15.7 (#683)

This commit is contained in:
marjune
2019-07-16 10:55:54 +08:00
committed by ᴊ. ᴄʜᴇɴ
parent 9580580bda
commit c78236269d
8 changed files with 438 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
// pipeline1.go
package main
import (
"os"
"text/template"
)
func main() {
t := template.New("template test")
t = template.Must(t.Parse("This is just static text. \n{{\"This is pipeline data - because it is evaluated within the double braces.\"}} {{`So is this, but within reverse quotes.`}}\n"))
t.Execute(os.Stdout, nil)
}
/*
This is just static text.
This is pipeline data - because it is evaluated within the double braces. So is this, but within reverse quotes.
*/

View File

@@ -0,0 +1,15 @@
// predefined_functions.go
package main
import (
"os"
"text/template"
)
func main() {
t := template.New("test")
t = template.Must(t.Parse("{{with $x := `hello`}}{{printf `%s %s` $x `Mary`}}{{end}}!\n"))
t.Execute(os.Stdout, nil)
}
// hello Mary!

View File

@@ -0,0 +1,24 @@
// template_field.go
package main
import (
"fmt"
"os"
"text/template"
)
type Person struct {
Name string
nonExportedAgeField string
}
func main() {
t := template.New("hello")
t, _ = t.Parse("hello {{.Name}}!")
p := Person{Name: "Mary", nonExportedAgeField: "31"}
if err := t.Execute(os.Stdout, p); err != nil {
fmt.Println("There was an error:", err.Error())
}
}
// Output: hello Mary!

View File

@@ -0,0 +1,27 @@
// template_ifelse.go
package main
import (
"os"
"text/template"
)
func main() {
tEmpty := template.New("template test")
tEmpty = template.Must(tEmpty.Parse("Empty pipeline if demo: {{if ``}} Will not print. {{end}}\n")) //empty pipeline following if
tEmpty.Execute(os.Stdout, nil)
tWithValue := template.New("template test")
tWithValue = template.Must(tWithValue.Parse("Non empty pipeline if demo: {{if `anything`}} Will print. {{end}}\n")) //non empty pipeline following if condition
tWithValue.Execute(os.Stdout, nil)
tIfElse := template.New("template test")
tIfElse = template.Must(tIfElse.Parse("if-else demo: {{if `anything`}} Print IF part. {{else}} Print ELSE part.{{end}}\n")) //non empty pipeline following if condition
tIfElse.Execute(os.Stdout, nil)
}
/* Output:
Empty pipeline if demo:
Non empty pipeline if demo: Will print.
if-else demo: Print IF part.
*/

View File

@@ -0,0 +1,23 @@
// template_validation.go
package main
import (
"text/template"
"fmt"
)
func main() {
tOk := template.New("ok")
//a valid template, so no panic with Must:
template.Must(tOk.Parse("/* and a comment */ some static text: {{ .Name }}"))
fmt.Println("The first one parsed OK.")
fmt.Println("The next one ought to fail.")
tErr := template.New("error_template")
template.Must(tErr.Parse(" some static text {{ .Name }"))
}
/* Output:
The first one parsed OK.
The next one ought to fail.
panic: template: error_template:1: unexpected "}" in command
*/

View File

@@ -0,0 +1,25 @@
// template_variables.go
package main
import (
"os"
"text/template"
)
func main() {
t := template.New("test")
t = template.Must(t.Parse("{{with $3 := `hello`}}{{$3}}{{end}}!\n"))
t.Execute(os.Stdout, nil)
t = template.Must(t.Parse("{{with $x3 := `hola`}}{{$x3}}{{end}}!\n"))
t.Execute(os.Stdout, nil)
t = template.Must(t.Parse("{{with $x_1 := `hey`}}{{$x_1}} {{.}} {{$x_1}}{{end}}!\n"))
t.Execute(os.Stdout, nil)
}
/* Output:
hello!
hola!
hey hey hey!
*/

View File

@@ -0,0 +1,21 @@
// template_with_end.go
package main
import (
"os"
"text/template"
)
func main() {
t := template.New("test")
t, _ = t.Parse("{{with `hello`}}{{.}}{{end}}!\n")
t.Execute(os.Stdout, nil)
t, _ = t.Parse("{{with `hello`}}{{.}} {{with `Mary`}}{{.}}{{end}}{{end}}!\n")
t.Execute(os.Stdout, nil)
}
/* Output:
hello!
hello Mary!
*/