Browse Source

Refactored isomorphic template rendering code

Wirecog, LLC 3 years ago
parent
commit
268d19e752
3 changed files with 25 additions and 25 deletions
  1. 8
    8
      template.go
  2. 7
    7
      templatebundle.go
  3. 10
    10
      templateset.go

+ 8
- 8
template.go View File

@@ -9,7 +9,7 @@ import (
9 9
 	"bytes"
10 10
 	"errors"
11 11
 	"html/template"
12
-	"net/http"
12
+	"io"
13 13
 	"strings"
14 14
 
15 15
 	"honnef.co/go/js/dom"
@@ -40,11 +40,11 @@ const (
40 40
 )
41 41
 
42 42
 type RenderParams struct {
43
-	Data           interface{}
44
-	ResponseWriter http.ResponseWriter
45
-	Element        dom.Element
46
-	Disposition    int8
47
-	Attributes     map[string]string
43
+	Data        interface{}
44
+	Writer      io.Writer
45
+	Element     dom.Element
46
+	Disposition int8
47
+	Attributes  map[string]string
48 48
 }
49 49
 
50 50
 func (t *Template) GetTemplateType() int8 {
@@ -82,7 +82,7 @@ func (t *Template) NameWithPrefix() string {
82 82
 
83 83
 func (t *Template) Render(params *RenderParams) error {
84 84
 
85
-	if OperatingEnvironment() == ServerEnvironment && (params.ResponseWriter == nil) {
85
+	if OperatingEnvironment() == ServerEnvironment && (params.Writer == nil) {
86 86
 		return errors.New("Either the response writer and/or the request is nil!")
87 87
 	}
88 88
 
@@ -135,7 +135,7 @@ func (t *Template) RenderTemplateOnClient(params *RenderParams) {
135 135
 
136 136
 func (t *Template) RenderTemplateOnServer(params *RenderParams) {
137 137
 
138
-	w := params.ResponseWriter
138
+	w := params.Writer
139 139
 
140 140
 	var tpl bytes.Buffer
141 141
 	if err := t.Execute(&tpl, params.Data); err != nil {

templatesetcontents.go → templatebundle.go View File

@@ -13,24 +13,24 @@ import (
13 13
 	"strings"
14 14
 )
15 15
 
16
-type TemplateSetContents struct {
16
+type TemplateBundle struct {
17 17
 	items map[string]string
18 18
 }
19 19
 
20
-func NewTemplateSetContents() *TemplateSetContents {
20
+func NewTemplateBundle() *TemplateBundle {
21 21
 
22
-	return &TemplateSetContents{
22
+	return &TemplateBundle{
23 23
 		items: map[string]string{},
24 24
 		// Funcs:   template.FuncMap{},
25 25
 	}
26 26
 
27 27
 }
28 28
 
29
-func (tsc *TemplateSetContents) Items() map[string]string {
30
-	return tsc.items
29
+func (t *TemplateBundle) Items() map[string]string {
30
+	return t.items
31 31
 }
32 32
 
33
-func (tsc *TemplateSetContents) importTemplateFileContents() error {
33
+func (t *TemplateBundle) importTemplateFileContents() error {
34 34
 
35 35
 	templateDirectory := filepath.Clean(TemplateFilesPath)
36 36
 
@@ -38,7 +38,7 @@ func (tsc *TemplateSetContents) importTemplateFileContents() error {
38 38
 		if strings.HasSuffix(path, TemplateFileExtension) {
39 39
 			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+string(os.PathSeparator)), TemplateFileExtension)
40 40
 			contents, err := ioutil.ReadFile(path)
41
-			tsc.items[name] = string(contents)
41
+			t.items[name] = string(contents)
42 42
 
43 43
 			if err != nil {
44 44
 				fmt.Println("error encountered while walking directory: ", err)

+ 10
- 10
templateset.go View File

@@ -12,9 +12,9 @@ import (
12 12
 )
13 13
 
14 14
 type TemplateSet struct {
15
-	members  map[string]*Template
16
-	Funcs    template.FuncMap
17
-	contents *TemplateSetContents
15
+	members map[string]*Template
16
+	Funcs   template.FuncMap
17
+	bundle  *TemplateBundle
18 18
 }
19 19
 
20 20
 func NewTemplateSet() *TemplateSet {
@@ -28,8 +28,8 @@ func (t *TemplateSet) Members() map[string]*Template {
28 28
 	return t.members
29 29
 }
30 30
 
31
-func (t *TemplateSet) Contents() *TemplateSetContents {
32
-	return t.contents
31
+func (t *TemplateSet) Bundle() *TemplateBundle {
32
+	return t.bundle
33 33
 }
34 34
 
35 35
 func (t *TemplateSet) AddTemplateFile(name, filename string, templateType int8) error {
@@ -101,7 +101,7 @@ func (t *TemplateSet) ImportTemplatesFromMap(templateMap map[string]string) erro
101 101
 	return nil
102 102
 }
103 103
 
104
-func (t *TemplateSet) RenderMember(templateName string, params *RenderParams) {
104
+func (t *TemplateSet) Render(templateName string, params *RenderParams) {
105 105
 
106 106
 	t.Members()[templateName].Render(params)
107 107
 
@@ -109,9 +109,9 @@ func (t *TemplateSet) RenderMember(templateName string, params *RenderParams) {
109 109
 
110 110
 func (t *TemplateSet) GatherTemplates() {
111 111
 
112
-	contents := NewTemplateSetContents()
113
-	contents.importTemplateFileContents()
114
-	t.ImportTemplatesFromMap(contents.Items())
115
-	t.contents = contents
112
+	bundle := NewTemplateBundle()
113
+	bundle.importTemplateFileContents()
114
+	t.ImportTemplatesFromMap(bundle.Items())
115
+	t.bundle = bundle
116 116
 
117 117
 }