Skip to content

Commit 5fd01ce

Browse files
committed
- [+] add more demo cases
1 parent d9c05a9 commit 5fd01ce

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

example_execute_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package easygen_test
22

33
import (
4+
"fmt"
45
"os"
6+
"strings"
57

68
"github.com/go-easygen/easygen"
79
"github.com/go-easygen/easygen/egCal"
@@ -15,21 +17,32 @@ type variable struct {
1517
// for standalone test, change package to `main` and the next func def to,
1618
// func main() {
1719
func ExampleExecute() {
20+
easygen.Opts.Debug = 1
21+
1822
tmpl0 := easygen.NewTemplate().Customize()
1923
tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs())
2024

2125
// define driving data of any tye
2226
v0 := variable{"some-init-method"}
23-
easygen.Opts.Debug = 1
24-
2527
// https://godoc.org/github.com/go-easygen/easygen#Execute
2628
// provide full template file name with extension
2729
easygen.Execute(tmpl, os.Stdout, "test/var0.tmpl", v0)
2830

31+
// Demo of using driving data of pure slice/array
32+
v1 := []string{"red", "blue", "white"}
33+
easygen.Execute(tmpl, os.Stdout, "test/list00.tmpl", v1)
34+
35+
// Demo output to string
36+
var b strings.Builder
37+
easygen.Execute(tmpl, &b, "test/list00f.tmpl", v1)
38+
fmt.Print(b.String())
39+
2940
// Output:
3041
// Input: "some-init-method"
3142
// Output 1: "SomeInitMethod"
3243
// Output 2: "SOME_INIT_METHOD"
44+
// The colors are: red, blue, white, .
45+
// The colors are: red, blue, white.
3346
}
3447

3548
// To show the full code in GoDoc

test/list00.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The colors are: {{range .}}{{.}}, {{end}}.

test/list00f.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The colors are: {{range $i, $color := .}}{{$color}}{{if lt $i ($ | len | minus1)}}, {{end}}{{end}}.

0 commit comments

Comments
 (0)