File: example_execute0_test.go

package info (click to toggle)
easygen 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: sh: 14; makefile: 13
file content (41 lines) | stat: -rw-r--r-- 1,121 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package easygen_test

import (
	"fmt"
	"os"
	"strings"

	"github.com/go-easygen/easygen"
)

// for standalone test, change package to `main` and the next func def to,
// func main() {
func ExampleExecute0() {
	easygen.Opts.Debug = 1

	tmpl := easygen.NewTemplate().Funcs(easygen.FuncDefs())

	// define driving data
	v0 := easygen.EgData{"v": "some-init-method"}
	// https://godoc.org/github.com/go-easygen/easygen#Execute0
	// provide template string, not file name
	easygen.Execute0(tmpl, os.Stdout, "{{stringsToUpper .v}}\n", v0)

	// Demo of using driving data of slice/array
	v1 := easygen.EgData{"v": []string{"red", "blue", "white"}}
	easygen.Execute0(tmpl, os.Stdout, "The colors are: {{range .v}}{{.}}, {{end}}.\n", v1)

	// Demo output to string
	var b strings.Builder
	easygen.Execute0(tmpl, &b, "The colors are: {{range $i, $color := .v}}{{$color}}{{if lt $i ($.v | len | minus1)}}, {{end}}{{end}}.\n", v1)
	fmt.Print(b.String())

	// Output:
	// SOME-INIT-METHOD
	// The colors are: red, blue, white, .
	// The colors are: red, blue, white.
}

// To show the full code in GoDoc
type dummyExecute0 struct {
}