From 767257423203f2c6ae3ad4903de06c664170d7b9 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 13 Mar 2023 14:39:11 +0200 Subject: [PATCH] view: allow only-custom template parsing --- view/amber.go | 7 +++++++ view/django.go | 5 +++++ view/handlebars.go | 7 +++++++ view/html.go | 10 +++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/view/amber.go b/view/amber.go index 91dae683..72c6d90b 100644 --- a/view/amber.go +++ b/view/amber.go @@ -13,6 +13,8 @@ import ( "sync" "sync/atomic" + "github.com/kataras/iris/v12/context" + "github.com/eknkc/amber" ) @@ -152,6 +154,11 @@ func (s *AmberEngine) AddFunc(funcName string, funcBody interface{}) { // // Returns an error if something bad happens, user is responsible to catch it. func (s *AmberEngine) Load() error { + // If only custom templates should be loaded. + if (s.fs == nil || context.IsNoOpFS(s.fs)) && len(s.templateCache) > 0 { + return nil + } + rootDirName := getRootDirName(s.fs) return walk(s.fs, "", func(path string, info os.FileInfo, err error) error { diff --git a/view/django.go b/view/django.go index 7706fa15..473a3b81 100644 --- a/view/django.go +++ b/view/django.go @@ -222,6 +222,11 @@ func (s *DjangoEngine) RegisterTag(tagName string, fn TagParser) error { // // Returns an error if something bad happens, user is responsible to catch it. func (s *DjangoEngine) Load() error { + // If only custom templates should be loaded. + if (s.fs == nil || context.IsNoOpFS(s.fs)) && len(s.templateCache) > 0 { + return nil + } + rootDirName := getRootDirName(s.fs) return walk(s.fs, "", func(path string, info os.FileInfo, err error) error { diff --git a/view/handlebars.go b/view/handlebars.go index a5af28fd..fce74a3e 100644 --- a/view/handlebars.go +++ b/view/handlebars.go @@ -10,6 +10,8 @@ import ( "strings" "sync" + "github.com/kataras/iris/v12/context" + "github.com/mailgun/raymond/v2" ) @@ -134,6 +136,11 @@ 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 { + // If only custom templates should be loaded. + if (s.fs == nil || context.IsNoOpFS(s.fs)) && len(s.templateCache) > 0 { + return nil + } + rootDirName := getRootDirName(s.fs) return walk(s.fs, "", func(path string, info os.FileInfo, _ error) error { diff --git a/view/html.go b/view/html.go index 1542434e..c6b10403 100644 --- a/view/html.go +++ b/view/html.go @@ -10,6 +10,8 @@ import ( "path/filepath" "strings" "sync" + + "github.com/kataras/iris/v12/context" ) // HTMLEngine contains the html view engine structure. @@ -61,7 +63,8 @@ var ( // Usage: // HTML("./views", ".html") or // HTML(iris.Dir("./views"), ".html") or -// HTML(embed.FS, ".html") or HTML(AssetFile(), ".html") for embedded data. +// HTML(embed.FS, ".html") or HTML(AssetFile(), ".html") for embedded data or +// HTML("","").ParseTemplate("hello", `[]byte("hello {{.Name}}")`, nil) for custom template parsing only. func HTML(dirOrFS interface{}, extension string) *HTMLEngine { s := &HTMLEngine{ name: "HTML", @@ -243,6 +246,11 @@ func (s *HTMLEngine) load() error { return err } + // If only custom templates should be loaded. + if (s.fs == nil || context.IsNoOpFS(s.fs)) && len(s.Templates.DefinedTemplates()) > 0 { + return nil + } + rootDirName := getRootDirName(s.fs) err := walk(s.fs, "", func(path string, info os.FileInfo, err error) error {