Browse Source

Added functionality to associate a function map to a template set

Wirecog 6 years ago
parent
commit
589a49bd82
2 changed files with 34 additions and 1 deletions
  1. 32
    0
      fetchtemplatebundle.go
  2. 2
    1
      templateset.go

+ 32
- 0
fetchtemplatebundle.go View File

3
 import (
3
 import (
4
 	"bytes"
4
 	"bytes"
5
 	"encoding/gob"
5
 	"encoding/gob"
6
+	"html/template"
6
 
7
 
7
 	"honnef.co/go/js/xhr"
8
 	"honnef.co/go/js/xhr"
8
 )
9
 )
36
 
37
 
37
 	templateSetChannel <- templateSet
38
 	templateSetChannel <- templateSet
38
 }
39
 }
40
+
41
+func FetchTemplateBundleWithSuppliedFunctionMap(templateSetChannel chan *TemplateSet, funcMap template.FuncMap) {
42
+
43
+	data, err := xhr.Send("POST", "/template-bundle", nil)
44
+	if err != nil {
45
+		println("Encountered error: ", err)
46
+		println(err)
47
+	}
48
+	var templateBundleMap map[string]string
49
+	var templateBundleMapBuffer bytes.Buffer
50
+
51
+	dec := gob.NewDecoder(&templateBundleMapBuffer)
52
+	templateBundleMapBuffer = *bytes.NewBuffer(data)
53
+	err = dec.Decode(&templateBundleMap)
54
+
55
+	if err != nil {
56
+		println("Encountered error: ", err)
57
+		panic(err)
58
+	}
59
+
60
+	templateSet := NewTemplateSet()
61
+	templateSet.Funcs = funcMap
62
+	err = templateSet.ImportTemplatesFromMap(templateBundleMap)
63
+
64
+	if err != nil {
65
+		println("Encountered import error: ", err)
66
+		panic(err)
67
+	}
68
+
69
+	templateSetChannel <- templateSet
70
+}

+ 2
- 1
templateset.go View File

8
 import (
8
 import (
9
 	"html/template"
9
 	"html/template"
10
 	"io/ioutil"
10
 	"io/ioutil"
11
+	"log"
11
 	"strings"
12
 	"strings"
12
 )
13
 )
13
 
14
 
86
 		tpl, err := template.New(name).Funcs(t.Funcs).Parse(templateContents)
87
 		tpl, err := template.New(name).Funcs(t.Funcs).Parse(templateContents)
87
 
88
 
88
 		if err != nil {
89
 		if err != nil {
89
-			println("Encountered error when attempting to parse template: ", err)
90
+			log.Println("Encountered error when attempting to parse template: ", err)
90
 
91
 
91
 			return err
92
 			return err
92
 		}
93
 		}