1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

add support for fs.FS, embed.FS (in addition of string and http.FileSystem) for i18n locales and view engine's templates

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-09-25 20:40:56 +03:00
parent 4cd0621018
commit 512ed6ffc0
29 changed files with 276 additions and 202 deletions

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"html/template"
"io"
"net/http"
"io/fs"
"os"
"path/filepath"
"strings"
@@ -15,7 +15,7 @@ import (
// HandlebarsEngine contains the handlebars view engine structure.
type HandlebarsEngine struct {
fs http.FileSystem
fs fs.FS
// files configuration
rootDir string
extension string
@@ -41,7 +41,7 @@ var (
// Usage:
// Handlebars("./views", ".html") or
// Handlebars(iris.Dir("./views"), ".html") or
// Handlebars(AssetFile(), ".html") for embedded data.
// Handlebars(embed.FS, ".html") or Handlebars(AssetFile(), ".html") for embedded data.
func Handlebars(fs interface{}, extension string) *HandlebarsEngine {
s := &HandlebarsEngine{
fs: getFS(fs),
@@ -66,6 +66,15 @@ func Handlebars(fs interface{}, extension string) *HandlebarsEngine {
// RootDir sets the directory to be used as a starting point
// to load templates from the provided file system.
func (s *HandlebarsEngine) RootDir(root string) *HandlebarsEngine {
if s.fs != nil && root != "" && root != "/" && root != "." && root != s.rootDir {
sub, err := fs.Sub(s.fs, s.rootDir)
if err != nil {
panic(err)
}
s.fs = sub // here so the "middleware" can work.
}
s.rootDir = filepath.ToSlash(root)
return s
}
@@ -125,7 +134,7 @@ func (s *HandlebarsEngine) AddGlobalFunc(funcName string, funcBody interface{})
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *HandlebarsEngine) Load() error {
return walk(s.fs, s.rootDir, func(path string, info os.FileInfo, _ error) error {
return walk(s.fs, "", func(path string, info os.FileInfo, _ error) error {
if info == nil || info.IsDir() {
return nil
}