-
Notifications
You must be signed in to change notification settings - Fork 0
/
templater.go
48 lines (40 loc) · 1.19 KB
/
templater.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
package templater
import (
"io/ioutil"
"bytes"
"fmt"
"log"
"github.com/tyler-sommer/stick"
)
var env *stick.Env
func init(){
fmt.Println("Starting Templater...")
env = stick.New(nil)
m := map[string]string{"user": "Vermin" , "permision": "Admin"}
m_typecast := StringMapToStickValue(m)
buff := new(bytes.Buffer)
if err := env.Execute("\tHello, {{permision}} {{ user }}!", buff, m_typecast ); err != nil {
log.Fatal(err)
}
fmt.Println(buff);
fmt.Println("...Templater Initialized")
}
func ReturnFilledTemplate(template_path string, value_map map[string]string) string{
template_bytes, err := ioutil.ReadFile(template_path)
if err != nil{
panic(err)
}
value_typecast := StringMapToStickValue(value_map)
template_buffer := new(bytes.Buffer)
if err := env.Execute(string(template_bytes), template_buffer, value_typecast ); err != nil {
log.Fatal(err)
}
return template_buffer.String()
}
func StringMapToStickValue(value_map map[string]string) map[string]stick.Value {
m_typecast := map[string]stick.Value{}
for key, value := range value_map {
m_typecast[key] = stick.Value(value)
}
return m_typecast
}