-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
52 lines (44 loc) · 1.22 KB
/
example_test.go
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
42
43
44
45
46
47
48
49
50
51
52
package templator_test
import (
"bytes"
"context"
"fmt"
"testing/fstest"
"github.com/alesr/templator"
)
func Example() {
// Create template files in memory
fs := fstest.MapFS{
"templates/home.html": &fstest.MapFile{
Data: []byte(`<h1>{{.Title}}</h1><p>{{.Content}}</p>`),
},
"templates/team.html": &fstest.MapFile{
Data: []byte(`<h2>{{.Title}}</h2><p>{{.Content}}</p>`),
},
}
// Create a new registry
reg, _ := templator.NewRegistry[templator.TestData](fs)
// Get and execute home template
home, _ := reg.Get("home")
var homeOutput bytes.Buffer
home.Execute(context.Background(), &homeOutput, templator.TestData{
Title: "Welcome",
Content: "Hello, World!",
})
// Get and execute team template
team, _ := reg.Get("team")
var teamOutput bytes.Buffer
team.Execute(context.Background(), &teamOutput, templator.TestData{
Title: "Engineering",
Content: "Building amazing things",
})
// Print the outputs
fmt.Printf("Home template output:\n%s\n\n", homeOutput.String())
fmt.Printf("Team template output:\n%s\n", teamOutput.String())
// Output:
// Home template output:
// <h1>Welcome</h1><p>Hello, World!</p>
//
// Team template output:
// <h2>Engineering</h2><p>Building amazing things</p>
}