Move template engines to middleware

pull/235/head
Fenny 2020-03-22 20:31:58 +01:00
parent ad9f3eccad
commit 83937bec52
5 changed files with 64 additions and 39 deletions

View File

@ -10,7 +10,8 @@ go:
- 1.12.x
- 1.13.x
- 1.14.x
env:
- GO111MODULE=on
script:
# build test for supported platforms
- GOOS=linux go build

View File

@ -53,7 +53,7 @@ type (
// Folder containing template files
TemplateFolder string `default:""`
// Template engine: html, amber, handlebars , mustache or pug
TemplateEngine string `default:""`
TemplateEngine func(raw string, bind interface{}) (out string, err error) `default:""`
// Extension for the template files
TemplateExtension string `default:""`
}

View File

@ -5,8 +5,10 @@
package fiber
import (
"bytes"
"encoding/xml"
"fmt"
"html/template"
"io/ioutil"
"log"
"mime"
@ -18,7 +20,6 @@ import (
"sync"
"time"
template "github.com/gofiber/template"
jsoniter "github.com/json-iterator/go"
fasthttp "github.com/valyala/fasthttp"
)
@ -720,15 +721,11 @@ func (ctx *Ctx) Render(file string, bind interface{}, engine ...string) error {
var err error
var raw []byte
var html string
var e string
if len(engine) > 0 {
e = engine[0]
} else if ctx.app.Settings.TemplateEngine != "" {
e = ctx.app.Settings.TemplateEngine
} else {
e = filepath.Ext(file)[1:]
log.Println("Warning: engine parameter is deprecated since v1.8.4, please use github.com/gofiber/template instead.")
}
if ctx.app.Settings.TemplateFolder != "" {
file = filepath.Join(ctx.app.Settings.TemplateFolder, file)
}
@ -738,32 +735,72 @@ func (ctx *Ctx) Render(file string, bind interface{}, engine ...string) error {
if raw, err = ioutil.ReadFile(filepath.Clean(file)); err != nil {
return err
}
if ctx.app.Settings.TemplateEngine != nil {
// Custom template engine
// https://github.com/gofiber/template
if html, err = ctx.app.Settings.TemplateEngine(getString(raw), bind); err != nil {
return err
}
} else {
// Default template engine
// https://golang.org/pkg/text/template/
var buf bytes.Buffer
var tmpl *template.Template
switch e {
case "amber": // https://github.com/eknkc/amber
if html, err = template.Amber(getString(raw), bind); err != nil {
if tmpl, err = template.New("").Parse(getString(raw)); err != nil {
return err
}
case "handlebars": // https://github.com/aymerick/raymond
if html, err = template.Handlebars(getString(raw), bind); err != nil {
return err
}
case "mustache": // https://github.com/cbroglie/mustache
if html, err = template.Mustache(getString(raw), bind); err != nil {
return err
}
case "pug": // https://github.com/Joker/jade
if html, err = template.Pug(getString(raw), bind); err != nil {
return err
}
default: // https://golang.org/pkg/text/template/
if html, err = template.HTML(getString(raw), bind); err != nil {
if err = tmpl.Execute(&buf, bind); err != nil {
return err
}
html = buf.String()
}
ctx.Set("Content-Type", "text/html")
ctx.SendString(html)
return err
// if len(engine) > 0 {
// log.Println("Deprectated")
// e = engine[0]
// } else if ctx.app.Settings.TemplateEngine != "" {
// e = ctx.app.Settings.TemplateEngine
// } else {
// e = filepath.Ext(file)[1:]
// }
// if ctx.app.Settings.TemplateFolder != "" {
// file = filepath.Join(ctx.app.Settings.TemplateFolder, file)
// }
// if ctx.app.Settings.TemplateExtension != "" {
// file = file + ctx.app.Settings.TemplateExtension
// }
// if raw, err = ioutil.ReadFile(filepath.Clean(file)); err != nil {
// return err
// }
// switch e {
// case "amber": // https://github.com/eknkc/amber
// if html, err = template.Amber(getString(raw), bind); err != nil {
// return err
// }
// case "handlebars": // https://github.com/aymerick/raymond
// if html, err = template.Handlebars(getString(raw), bind); err != nil {
// return err
// }
// case "mustache": // https://github.com/cbroglie/mustache
// if html, err = template.Mustache(getString(raw), bind); err != nil {
// return err
// }
// case "pug": // https://github.com/Joker/jade
// if html, err = template.Pug(getString(raw), bind); err != nil {
// return err
// }
// default: // https://golang.org/pkg/text/template/
// if html, err = template.HTML(getString(raw), bind); err != nil {
// return err
// }
// }
// ctx.Set("Content-Type", "text/html")
// ctx.SendString(html)
// return err
}
// Returns the matched Route struct.

1
go.mod
View File

@ -3,7 +3,6 @@ module github.com/gofiber/fiber
go 1.11
require (
github.com/gofiber/template v1.0.0
github.com/gorilla/schema v1.1.0
github.com/json-iterator/go v1.1.9
github.com/valyala/fasthttp v1.9.0

12
go.sum
View File

@ -1,16 +1,5 @@
github.com/Joker/hpp v0.0.0-20180418125244-6893e659854a/go.mod h1:MzD2WMdSxvbHw5fM/OXOFily/lipJWRc9C1px0Mt0ZE=
github.com/Joker/jade v1.0.0 h1:lOCEPvTAtWfLpSZYMOv/g44MGQFAolbKh2khHHGu0Kc=
github.com/Joker/jade v1.0.0/go.mod h1:efZIdO0py/LtcJRSa/j2WEklMSAw84WV0zZVMxNToB8=
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/cbroglie/mustache v1.0.1 h1:ivMg8MguXq/rrz2eu3tw6g3b16+PQhoTn6EZAhst2mw=
github.com/cbroglie/mustache v1.0.1/go.mod h1:R/RUa+SobQ14qkP4jtx5Vke5sDytONDQXNLPY/PO69g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/gofiber/template v1.0.0 h1:Vf4Fby9zUWVQyY2y69KKyRHsEYlIE+Pxb25M+jiaEL0=
github.com/gofiber/template v1.0.0/go.mod h1:+bij+R0NI6urTg2jtQvPj5wb2uWMxW9eYGsAN3QhnP0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
@ -33,7 +22,6 @@ github.com/valyala/fasthttp v1.9.0 h1:hNpmUdy/+ZXYpGy0OBfm7K0UQTzb73W0T0U4iJIVrM
github.com/valyala/fasthttp v1.9.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=