-
Notifications
You must be signed in to change notification settings - Fork 8
/
views.go
231 lines (190 loc) · 6.29 KB
/
views.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright © 2019, 2020 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <[email protected]>
*/
package kaliber
//lint:file-ignore ST1017 - I prefer Yoda conditions
/*
* This file provides some template/view related functions and methods.
*/
import (
"bytes"
"fmt"
"html/template"
"io"
"net/http"
"path/filepath"
"regexp"
"github.com/mwat56/kaliber/db"
"github.com/mwat56/whitespace"
)
const (
// replacement text for `reHrefRE`
reHrefReplace = ` target="_extern" $1`
)
var (
// RegEx to HREF= tag attributes
reHrefRE = regexp.MustCompile(` (href="http)`)
)
// `addExternURLtargets()` adds a TARGET attribute to HREFs.
func addExternURLtargets(aPage []byte) []byte {
return reHrefRE.ReplaceAll(aPage, []byte(reHrefReplace))
} // addExternURLtargets()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// `htmlSafe()` returns `aText` as template.HTML.
func htmlSafe(aText string) template.HTML {
return template.HTML(aText) // #nosec G203
} // htmlSafe()
// `selectOption()` returns the OPTION markup for `aValue`.
func selectOption(aMap *db.TStringMap, aValue string) template.HTML {
if result, ok := (*aMap)[aValue]; ok {
return template.HTML(result) // #nosec G203
}
return ""
} // selectOption()
var (
// A list of functions to be used from within templates;
// see `NewView()`.
viewFunctionMap = template.FuncMap{
"htmlSafe": htmlSafe, // returns `aText` as template.HTML
"selectOption": selectOption, // returns a Select Option
}
)
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// TView combines a template and its logical name.
type TView struct {
// The view's symbolic name.
tvName string
// The template as returned by a `NewView()` function call.
tvTpl *template.Template
}
// NewView returns a new `TView` with `aName`.
//
// `aBaseDir` is the path to the directory storing the template files.
//
// `aName` is the name of the template file providing the page's main
// body without the filename extension (i.e. w/o ".gohtml"). `aName`
// serves as both the main template's name as well as the view's name.
func NewView(aBaseDir, aName string) (*TView, error) {
bd, err := filepath.Abs(aBaseDir)
if nil != err {
return nil, err
}
files, err := filepath.Glob(bd + "/layout/*.gohtml")
if nil != err {
return nil, err
}
files = append(files, bd+"/"+aName+".gohtml")
tpl, err := template.New(aName).
Funcs(viewFunctionMap).
ParseFiles(files...)
if nil != err {
return nil, err
}
return &TView{
tvName: aName,
tvTpl: tpl,
}, nil
} // NewView()
// `render()` is the core of `Render()` with a slightly different API
// (`io.Writer` instead of `http.ResponseWriter`) for easier testing.
func (v *TView) render(aWriter io.Writer, aData *TemplateData) (rErr error) {
var page []byte
if page, rErr = v.RenderedPage(aData); nil != rErr {
return
}
_, rErr = aWriter.Write(addExternURLtargets(whitespace.Remove(page)))
return
} // render()
// Render executes the template using the TView's properties.
//
// `aWriter` is a http.ResponseWriter, or e.g. `os.Stdout` in console apps.
//
// `aData` is a list of data to be injected into the template.
//
// If an error occurs executing the template or writing its output,
// execution stops, and the method returns without writing anything
// to the output `aWriter`.
func (v *TView) Render(aWriter http.ResponseWriter, aData *TemplateData) error {
return v.render(aWriter, aData)
} // Render()
// RenderedPage returns the rendered template/page and a possible Error
// executing the template.
//
// `aData` is a list of data to be injected into the template.
func (v *TView) RenderedPage(aData *TemplateData) ([]byte, error) {
buf := &bytes.Buffer{}
if err := v.tvTpl.ExecuteTemplate(buf, v.tvName, aData); nil != err {
return nil, err
}
return buf.Bytes(), nil
} // RenderedPage()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
type (
tViewList map[string]*TView
// TViewList is a list of `TView` instances (to be used as a template pool).
TViewList tViewList
)
// NewViewList returns a new (empty) `TViewList` instance.
func NewViewList() *TViewList {
result := make(TViewList, 16)
return &result
} // NewViewlist()
// Add appends `aView` to the list.
//
// `aView` is the view to add to this list.
//
// The view's name (as specified in the `NewView()` function call)
// is used as the view's key in this list.
func (vl *TViewList) Add(aView *TView) *TViewList {
(*vl)[aView.tvName] = aView
return vl
} // Add()
// Get returns the view with `aName`.
//
// `aName` is the name (key) of the `TView` object to retrieve.
//
// If `aName` doesn't exist, the return value is `nil`.
// The second value (ok) is a `bool` that is `true` if `aName`
// exists in the list, and `false` if not.
func (vl *TViewList) Get(aName string) (*TView, bool) {
if result, ok := (*vl)[aName]; ok {
return result, true
}
return nil, false
} // Get()
// `render()` is the core of `Render()` with a slightly different API
// (`io.Writer` instead of `http.ResponseWriter`) for easier testing.
func (vl *TViewList) render(aName string, aWriter io.Writer, aData *TemplateData) error {
if view, ok := (*vl)[aName]; ok {
return view.render(aWriter, aData)
}
return fmt.Errorf("template/view '%s' not found", aName)
} // render()
// Render executes the template with the key `aName`.
//
// `aName` is the name of the template/view to use.
//
// `aWriter` is a `http.ResponseWriter` to handle the executed template.
//
// `aData` is a list of data to be injected into the template.
//
// If an error occurs executing the template or writing its output,
// execution stops, and the method returns without writing anything
// to the output `aWriter`.
func (vl *TViewList) Render(aName string, aWriter http.ResponseWriter, aData *TemplateData) error {
return vl.render(aName, aWriter, aData)
} // Render()
// RenderedPage returns the rendered template/page with the key `aName`.
//
// `aName` is the name of the template/view to use.
//
// `aData` is a list of data to be injected into the template.
func (vl *TViewList) RenderedPage(aName string, aData *TemplateData) ([]byte, error) {
if view, ok := (*vl)[aName]; ok {
return view.RenderedPage(aData)
}
return nil, fmt.Errorf("template/view '%s' not found", aName)
} // RenderedPage()
/* _EoF_ */