-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault_engine.go
More file actions
41 lines (34 loc) · 1.06 KB
/
Copy pathdefault_engine.go
File metadata and controls
41 lines (34 loc) · 1.06 KB
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 tmplx
import (
"fmt"
"io"
)
var (
DefaultEngine *TemplateEngine
)
// H is a shortcut for map[string]interface{}
type H map[string]any
// Load initializes the default template engine and loads all templates
func Load(opts Options) error {
DefaultEngine = New(opts)
if err := DefaultEngine.Load(); err != nil {
return fmt.Errorf("error loading tmplx engine: %v", err)
}
return nil
}
// Render renders a template and returns the output as a string.
// Returns an error if Load has not been called first.
func Render(name string, data H) (string, error) {
if DefaultEngine == nil {
return "", fmt.Errorf("tmplx: DefaultEngine not initialized, call Load() first")
}
return DefaultEngine.Render(name, data)
}
// RenderResponse renders a template and writes it to the response writer.
// Returns an error if Load has not been called first.
func RenderResponse(w io.Writer, name string, data H) error {
if DefaultEngine == nil {
return fmt.Errorf("tmplx: DefaultEngine not initialized, call Load() first")
}
return DefaultEngine.RenderResponse(w, name, data)
}