mirror of
https://github.com/gin-gonic/gin.git
synced 2025-08-26 09:26:57 +08:00
feat(fs): Implement loading HTML from http.FileSystem (#4053)
* Implement loading HTML from http.FileSystem * Add OnlyHTMLFS test * Move OnlyHTMLFS to internal and add test
This commit is contained in:
@ -7,6 +7,8 @@ package render
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin/internal/fs"
|
||||
)
|
||||
|
||||
// Delims represents a set of Left and Right delimiters for HTML template rendering.
|
||||
@ -31,10 +33,12 @@ type HTMLProduction struct {
|
||||
|
||||
// HTMLDebug contains template delims and pattern and function with file list.
|
||||
type HTMLDebug struct {
|
||||
Files []string
|
||||
Glob string
|
||||
Delims Delims
|
||||
FuncMap template.FuncMap
|
||||
Files []string
|
||||
Glob string
|
||||
FileSystem http.FileSystem
|
||||
Patterns []string
|
||||
Delims Delims
|
||||
FuncMap template.FuncMap
|
||||
}
|
||||
|
||||
// HTML contains template reference and its name with given interface object.
|
||||
@ -73,7 +77,11 @@ func (r HTMLDebug) loadTemplate() *template.Template {
|
||||
if r.Glob != "" {
|
||||
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
|
||||
}
|
||||
panic("the HTML debug render was created without files or glob pattern")
|
||||
if r.FileSystem != nil && len(r.Patterns) > 0 {
|
||||
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFS(
|
||||
fs.FileSystem{FileSystem: r.FileSystem}, r.Patterns...))
|
||||
}
|
||||
panic("the HTML debug render was created without files or glob pattern or file system with patterns")
|
||||
}
|
||||
|
||||
// Render (HTML) executes template and writes its result with custom ContentType for response.
|
||||
|
Reference in New Issue
Block a user