Browse Source

Added functionality to associate a function map to a template set

Wirecog 7 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,6 +3,7 @@ package isokit
3 3
 import (
4 4
 	"bytes"
5 5
 	"encoding/gob"
6
+	"html/template"
6 7
 
7 8
 	"honnef.co/go/js/xhr"
8 9
 )
@@ -36,3 +37,34 @@ func FetchTemplateBundle(templateSetChannel chan *TemplateSet) {
36 37
 
37 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,6 +8,7 @@ package isokit
8 8
 import (
9 9
 	"html/template"
10 10
 	"io/ioutil"
11
+	"log"
11 12
 	"strings"
12 13
 )
13 14
 
@@ -86,7 +87,7 @@ func (t *TemplateSet) ImportTemplatesFromMap(templateMap map[string]string) erro
86 87
 		tpl, err := template.New(name).Funcs(t.Funcs).Parse(templateContents)
87 88
 
88 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 92
 			return err
92 93
 		}