Browse Source

Refactored isomorphic template rendering code

Wirecog 7 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
 	"bytes"
9
 	"bytes"
10
 	"errors"
10
 	"errors"
11
 	"html/template"
11
 	"html/template"
12
-	"net/http"
12
+	"io"
13
 	"strings"
13
 	"strings"
14
 
14
 
15
 	"honnef.co/go/js/dom"
15
 	"honnef.co/go/js/dom"
40
 )
40
 )
41
 
41
 
42
 type RenderParams struct {
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
 func (t *Template) GetTemplateType() int8 {
50
 func (t *Template) GetTemplateType() int8 {
82
 
82
 
83
 func (t *Template) Render(params *RenderParams) error {
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
 		return errors.New("Either the response writer and/or the request is nil!")
86
 		return errors.New("Either the response writer and/or the request is nil!")
87
 	}
87
 	}
88
 
88
 
135
 
135
 
136
 func (t *Template) RenderTemplateOnServer(params *RenderParams) {
136
 func (t *Template) RenderTemplateOnServer(params *RenderParams) {
137
 
137
 
138
-	w := params.ResponseWriter
138
+	w := params.Writer
139
 
139
 
140
 	var tpl bytes.Buffer
140
 	var tpl bytes.Buffer
141
 	if err := t.Execute(&tpl, params.Data); err != nil {
141
 	if err := t.Execute(&tpl, params.Data); err != nil {

templatesetcontents.go → templatebundle.go View File

13
 	"strings"
13
 	"strings"
14
 )
14
 )
15
 
15
 
16
-type TemplateSetContents struct {
16
+type TemplateBundle struct {
17
 	items map[string]string
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
 		items: map[string]string{},
23
 		items: map[string]string{},
24
 		// Funcs:   template.FuncMap{},
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
 	templateDirectory := filepath.Clean(TemplateFilesPath)
35
 	templateDirectory := filepath.Clean(TemplateFilesPath)
36
 
36
 
38
 		if strings.HasSuffix(path, TemplateFileExtension) {
38
 		if strings.HasSuffix(path, TemplateFileExtension) {
39
 			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+string(os.PathSeparator)), TemplateFileExtension)
39
 			name := strings.TrimSuffix(strings.TrimPrefix(path, templateDirectory+string(os.PathSeparator)), TemplateFileExtension)
40
 			contents, err := ioutil.ReadFile(path)
40
 			contents, err := ioutil.ReadFile(path)
41
-			tsc.items[name] = string(contents)
41
+			t.items[name] = string(contents)
42
 
42
 
43
 			if err != nil {
43
 			if err != nil {
44
 				fmt.Println("error encountered while walking directory: ", err)
44
 				fmt.Println("error encountered while walking directory: ", err)

+ 10
- 10
templateset.go View File

12
 )
12
 )
13
 
13
 
14
 type TemplateSet struct {
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
 func NewTemplateSet() *TemplateSet {
20
 func NewTemplateSet() *TemplateSet {
28
 	return t.members
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
 func (t *TemplateSet) AddTemplateFile(name, filename string, templateType int8) error {
35
 func (t *TemplateSet) AddTemplateFile(name, filename string, templateType int8) error {
101
 	return nil
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
 	t.Members()[templateName].Render(params)
106
 	t.Members()[templateName].Render(params)
107
 
107
 
109
 
109
 
110
 func (t *TemplateSet) GatherTemplates() {
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
 }