Skip to content

Commit 89d1b3b

Browse files
committed
minor: add AddFunc and Ext methods and renmae all interface{} to any keyword
1 parent a6f1fb2 commit 89d1b3b

File tree

12 files changed

+48
-33
lines changed

12 files changed

+48
-33
lines changed

Diff for: DOC.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040

4141
var views = blocks.New("./views").
4242
Reload(true).
43-
Funcs(map[string]interface{}{
43+
Funcs(map[string]any{
4444
"year": func() int {
4545
return time.Now().Year()
4646
},
@@ -62,7 +62,7 @@ func main() {
6262
func index(w http.ResponseWriter, r *http.Request) {
6363
w.Header().Set("Content-Type", "text/html; charset=utf-8")
6464

65-
data := map[string]interface{}{
65+
data := map[string]any{
6666
"Title": "Page Title",
6767
}
6868

@@ -76,7 +76,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
7676
w.Header().Set("Content-Type", "text/html; charset=utf-8")
7777
w.WriteHeader(http.StatusInternalServerError)
7878

79-
data := map[string]interface{}{
79+
data := map[string]any{
8080
"Code": http.StatusInternalServerError,
8181
"Message": "Internal Server Error",
8282
}
@@ -181,7 +181,7 @@ var views = blocks.New(embeddedFS).
181181
RootDir("data/views").
182182
Reload(true).
183183
LayoutDir("layouts").
184-
Funcs(map[string]interface{}{
184+
Funcs(map[string]any{
185185
"year": func() int {
186186
return time.Now().Year()
187187
},
@@ -203,7 +203,7 @@ func main() {
203203
func index(w http.ResponseWriter, r *http.Request) {
204204
w.Header().Set("Content-Type", "text/html; charset=utf-8")
205205

206-
data := map[string]interface{}{
206+
data := map[string]any{
207207
"Title": "Page Title",
208208
}
209209

@@ -217,7 +217,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
217217
w.Header().Set("Content-Type", "text/html; charset=utf-8")
218218
w.WriteHeader(http.StatusInternalServerError)
219219

220-
data := map[string]interface{}{
220+
data := map[string]any{
221221
"Code": http.StatusInternalServerError,
222222
"Message": "Internal Server Error",
223223
}

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ After the initialization and engine's customizations the user SHOULD call its [L
101101
err := views.Load()
102102
```
103103

104-
To render a template through a compatible [io.Writer](https://golang.org/pkg/io/#Writer) use the [ExecuteTemplate(w io.Writer, tmplName, layoutName string, data interface{})](https://pkg.go.dev/github.com/kataras/blocks?tab=doc#Blocks.ExecuteTemplate) method.
104+
To render a template through a compatible [io.Writer](https://golang.org/pkg/io/#Writer) use the [ExecuteTemplate(w io.Writer, tmplName, layoutName string, data any)](https://pkg.go.dev/github.com/kataras/blocks?tab=doc#Blocks.ExecuteTemplate) method.
105105

106106
```go
107107
func handler(w http.ResponseWriter, r *http.Request) {
108-
data := map[string]interface{}{
108+
data := map[string]any{
109109
"Title": "Index Title",
110110
}
111111

Diff for: _examples/basic/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
var views = blocks.New("./views").
1111
Reload(true).
12-
Funcs(map[string]interface{}{
12+
Funcs(map[string]any{
1313
"year": func() int {
1414
return time.Now().Year()
1515
},
@@ -31,7 +31,7 @@ func main() {
3131
func index(w http.ResponseWriter, r *http.Request) {
3232
w.Header().Set("Content-Type", "text/html; charset=utf-8")
3333

34-
data := map[string]interface{}{
34+
data := map[string]any{
3535
"Title": "Page Title",
3636
}
3737

@@ -45,7 +45,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
4545
w.Header().Set("Content-Type", "text/html; charset=utf-8")
4646
w.WriteHeader(http.StatusInternalServerError)
4747

48-
data := map[string]interface{}{
48+
data := map[string]any{
4949
"Code": http.StatusInternalServerError,
5050
"Message": "Internal Server Error",
5151
}

Diff for: _examples/embedded-bindata/bindata.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: _examples/embedded-bindata/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
var views = blocks.New(AssetFile()).
2121
Reload(true).
2222
LayoutDir("layouts").
23-
Funcs(map[string]interface{}{
23+
Funcs(map[string]any{
2424
"year": func() int {
2525
return time.Now().Year()
2626
},
@@ -42,7 +42,7 @@ func main() {
4242
func index(w http.ResponseWriter, r *http.Request) {
4343
w.Header().Set("Content-Type", "text/html; charset=utf-8")
4444

45-
data := map[string]interface{}{
45+
data := map[string]any{
4646
"Title": "Page Title",
4747
}
4848

@@ -56,7 +56,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
5656
w.Header().Set("Content-Type", "text/html; charset=utf-8")
5757
w.WriteHeader(http.StatusInternalServerError)
5858

59-
data := map[string]interface{}{
59+
data := map[string]any{
6060
"Code": http.StatusInternalServerError,
6161
"Message": "Internal Server Error",
6262
}

Diff for: _examples/embedded/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var views = blocks.New(embeddedFS).
1616
RootDir("data/views").
1717
Reload(true).
1818
LayoutDir("layouts").
19-
Funcs(map[string]interface{}{
19+
Funcs(map[string]any{
2020
"year": func() int {
2121
return time.Now().Year()
2222
},
@@ -38,7 +38,7 @@ func main() {
3838
func index(w http.ResponseWriter, r *http.Request) {
3939
w.Header().Set("Content-Type", "text/html; charset=utf-8")
4040

41-
data := map[string]interface{}{
41+
data := map[string]any{
4242
"Title": "Page Title",
4343
}
4444

@@ -52,7 +52,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
5252
w.Header().Set("Content-Type", "text/html; charset=utf-8")
5353
w.WriteHeader(http.StatusInternalServerError)
5454

55-
data := map[string]interface{}{
55+
data := map[string]any{
5656
"Code": http.StatusInternalServerError,
5757
"Message": "Internal Server Error",
5858
}

Diff for: _examples/funcs/mycollection/mycollection.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/kataras/blocks"
99
)
1010

11-
var funcs = template.FuncMap{ // map[string]interface{}
11+
var funcs = template.FuncMap{ // map[string]any
1212
"sub": func(i, j int) int {
1313
return i - j
1414
},
@@ -19,5 +19,5 @@ func init() {
1919
}
2020

2121
// Note: as a special feature, the function input's can be a type of
22-
// func(*Blocks) (fn interface{}) or func(*Blocks) template.FuncMap where you
22+
// func(*Blocks) (fn any) or func(*Blocks) template.FuncMap where you
2323
// need access to the current Blocks view engine.

Diff for: _examples/multiple/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
}
3737

3838
func index(w http.ResponseWriter, r *http.Request) {
39-
data := map[string]interface{}{
39+
data := map[string]any{
4040
"Title": "Page Title",
4141
}
4242

@@ -45,7 +45,7 @@ func index(w http.ResponseWriter, r *http.Request) {
4545

4646
func admin(v *blocks.Blocks) http.HandlerFunc {
4747
return func(w http.ResponseWriter, r *http.Request) {
48-
data := map[string]interface{}{
48+
data := map[string]any{
4949
"Title": "Admin Panel",
5050
}
5151
err := v.ExecuteTemplate(w, "index", "main", data)

Diff for: _examples/static-generator/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
const outputDir = "./public"
1616

17-
type data = map[string]interface{}
17+
type data = map[string]any
1818

1919
var defaultFuncs = data{
2020
"year": func() int {
@@ -74,7 +74,7 @@ func main() {
7474
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("./public"))))
7575
}
7676

77-
func readConfig(dest map[string]interface{}) error {
77+
func readConfig(dest map[string]any) error {
7878
f, err := os.Open("./site.yml")
7979
if err != nil {
8080
return err

Diff for: blocks.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type Blocks struct {
8787
// New("./views") or
8888
// New(http.Dir("./views")) or
8989
// New(embeddedFS) or New(AssetFile()) for embedded data.
90-
func New(fs interface{}) *Blocks {
90+
func New(fs any) *Blocks {
9191
v := &Blocks{
9292
fs: getFS(fs),
9393
layoutDir: "/layouts",
@@ -202,6 +202,21 @@ func (v *Blocks) Funcs(funcMap template.FuncMap) *Blocks {
202202
return v
203203
}
204204

205+
// AddFunc adds a function to the root template's function map.
206+
func (v *Blocks) AddFunc(funcName string, fn any) {
207+
if v.tmplFuncs == nil {
208+
v.tmplFuncs = make(template.FuncMap)
209+
}
210+
211+
v.tmplFuncs[funcName] = fn
212+
v.Root.Funcs(v.tmplFuncs)
213+
}
214+
215+
// Ext returns the template file extension (with dot).
216+
func (v *Blocks) Ext() string {
217+
return v.extension
218+
}
219+
205220
// LayoutFuncs same as `Funcs` but this map's elements will be added
206221
// only to the layout templates. It's legal to override elements of the root `Funcs`.
207222
func (v *Blocks) LayoutFuncs(funcMap template.FuncMap) *Blocks {
@@ -513,7 +528,7 @@ func (v *Blocks) load(ctx context.Context) error {
513528
//
514529
// A template may be executed safely in parallel, although if parallel
515530
// executions share a Writer the output may be interleaved.
516-
func (v *Blocks) ExecuteTemplate(w io.Writer, tmplName, layoutName string, data interface{}) error {
531+
func (v *Blocks) ExecuteTemplate(w io.Writer, tmplName, layoutName string, data any) error {
517532
if v.reload {
518533
if err := v.Load(); err != nil {
519534
return err
@@ -527,7 +542,7 @@ func (v *Blocks) ExecuteTemplate(w io.Writer, tmplName, layoutName string, data
527542
return v.executeTemplate(w, tmplName, layoutName, data)
528543
}
529544

530-
func (v *Blocks) executeTemplate(w io.Writer, tmplName, layoutName string, data interface{}) error {
545+
func (v *Blocks) executeTemplate(w io.Writer, tmplName, layoutName string, data any) error {
531546
if layoutName != "" {
532547
tmpl := v.getTemplateWithLayout(tmplName, layoutName)
533548
if tmpl == nil {
@@ -561,7 +576,7 @@ func (v *Blocks) executeTemplate(w io.Writer, tmplName, layoutName string, data
561576
// ParseTemplate parses a template based on its "tmplName" name and returns the result.
562577
// Note that, this does not reload the templates on each call if Reload was set to true.
563578
// To refresh the templates you have to manually call the `Load` upfront.
564-
func (v *Blocks) ParseTemplate(tmplName, layoutName string, data interface{}) (string, error) {
579+
func (v *Blocks) ParseTemplate(tmplName, layoutName string, data any) (string, error) {
565580
b := v.bufferPool.Get()
566581
// use the unexported method so it does not re-reload the templates on each partial one
567582
// when Reload was set to true.
@@ -572,7 +587,7 @@ func (v *Blocks) ParseTemplate(tmplName, layoutName string, data interface{}) (s
572587
}
573588

574589
// PartialFunc returns the parsed result of the "partialName" template's "content" block.
575-
func (v *Blocks) PartialFunc(partialName string, data interface{}) (template.HTML, error) {
590+
func (v *Blocks) PartialFunc(partialName string, data any) (template.HTML, error) {
576591
// contents, err := v.ParseTemplate(partialName, "content", data)
577592
// if err != nil {
578593
// return "", err

Diff for: fs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"path/filepath"
99
)
1010

11-
func getFS(fsOrDir interface{}) fs.FS {
11+
func getFS(fsOrDir any) fs.FS {
1212
switch v := fsOrDir.(type) {
1313
case string:
1414
return os.DirFS(v)

Diff for: funcs.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package blocks
33
import "html/template"
44

55
var builtins = template.FuncMap{
6-
"partial": func(v *Blocks) interface{} {
6+
"partial": func(v *Blocks) any {
77
return v.PartialFunc
88
},
99
}
@@ -13,7 +13,7 @@ var builtins = template.FuncMap{
1313
// The values (functions) should be compatible
1414
// with a standard html/template function, however
1515
// as a special feature, the function input's can be a type of
16-
// func(*Blocks) (fn interface{}) or func(*Blocks) template.FuncMap as well,
16+
// func(*Blocks) (fn any) or func(*Blocks) template.FuncMap as well,
1717
// so it can use the current engine's methods such as `ParseTemplate`.
1818
// It's legal to override previous functions.
1919
//
@@ -53,7 +53,7 @@ func translateFuncs(v *Blocks, funcMap template.FuncMap) template.FuncMap { // u
5353
}
5454

5555
switch f := fn.(type) {
56-
case func(*Blocks) interface{}:
56+
case func(*Blocks) any:
5757
funcs[name] = f(v)
5858
case func(*Blocks) template.FuncMap:
5959
for deepName, deepFn := range translateFuncs(v, f(v)) {

0 commit comments

Comments
 (0)