Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) | ||
// aahframework.org/view source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package view | ||
|
||
import ( | ||
"html/template" | ||
"path/filepath" | ||
"strings" | ||
|
||
"aahframework.org/log.v0" | ||
) | ||
|
||
// tmplSafeHTML method outputs given HTML as-is, use it with care. | ||
func (e *GoViewEngine) tmplSafeHTML(str string) template.HTML { | ||
return template.HTML(str) | ||
} | ||
|
||
// tmplInclude method renders given template with View Args and imports into | ||
// current template. | ||
func (e *GoViewEngine) tmplInclude(name string, viewArgs map[string]interface{}) template.HTML { | ||
if !strings.HasPrefix(name, "common") { | ||
name = "common/" + name | ||
} | ||
|
||
name = filepath.ToSlash(name) | ||
var err error | ||
var tmpl *template.Template | ||
if e.hotReload { | ||
if tmpl, err = e.ParseFile(name); err != nil { | ||
log.Errorf("goviewengine: %s", err) | ||
return e.tmplSafeHTML("") | ||
} | ||
} else { | ||
tmpl = commonTemplates.Lookup(name) | ||
} | ||
|
||
if tmpl == nil { | ||
log.Warnf("goviewengine: common template not found: %s", name) | ||
return e.tmplSafeHTML("") | ||
} | ||
|
||
buf := acquireBuffer() | ||
defer releaseBuffer(buf) | ||
if err = tmpl.Execute(buf, viewArgs); err != nil { | ||
log.Errorf("goviewengine: %s", err) | ||
return e.tmplSafeHTML("") | ||
} | ||
|
||
return e.tmplSafeHTML(buf.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) | ||
// aahframework.org/view source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package view | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
|
||
"aahframework.org/config.v0" | ||
"aahframework.org/log.v0" | ||
"aahframework.org/vfs.v0" | ||
) | ||
|
||
const noLayout = "nolayout" | ||
|
||
var ( | ||
commonTemplates *Templates | ||
bufPool *sync.Pool | ||
) | ||
|
||
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ | ||
// type GoViewEngine and its method | ||
//______________________________________________________________________________ | ||
|
||
// GoViewEngine implements the partial inheritance support with Go templates. | ||
type GoViewEngine struct { | ||
*EngineBase | ||
} | ||
|
||
// Init method initialize a template engine with given aah application config | ||
// and application views base path. | ||
func (e *GoViewEngine) Init(fs *vfs.VFS, appCfg *config.Config, baseDir string) error { | ||
if e.EngineBase == nil { | ||
e.EngineBase = new(EngineBase) | ||
} | ||
|
||
if err := e.EngineBase.Init(fs, appCfg, baseDir, "go", ".html"); err != nil { | ||
return err | ||
} | ||
|
||
// Add template func | ||
AddTemplateFunc(template.FuncMap{ | ||
"safeHTML": e.tmplSafeHTML, | ||
"import": e.tmplInclude, | ||
"include": e.tmplInclude, // alias for import | ||
}) | ||
|
||
// load common templates | ||
if err := e.loadCommonTemplates(); err != nil { | ||
return err | ||
} | ||
|
||
// collect all layouts | ||
layouts, err := e.LayoutFiles() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// load layout templates | ||
if err = e.loadLayoutTemplates(layouts); err != nil { | ||
return err | ||
} | ||
|
||
if !e.IsLayoutEnabled { | ||
// since pages directory processed above, no error expected here | ||
_ = e.loadNonLayoutTemplates("pages") | ||
} | ||
|
||
if e.VFS.IsExists(filepath.Join(e.BaseDir, "errors")) { | ||
if err = e.loadNonLayoutTemplates("errors"); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ | ||
// GoViewEngine unexported methods | ||
//______________________________________________________________________________ | ||
|
||
func (e *GoViewEngine) loadCommonTemplates() error { | ||
commons, err := e.FilesPath("common") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
commonTemplates = &Templates{} | ||
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }} | ||
prefix := path.Dir(e.BaseDir) | ||
for _, file := range commons { | ||
if !strings.HasSuffix(file, e.FileExt) { | ||
log.Warnf("goviewengine: not a valid template extension[%s]: %s", e.FileExt, TrimPathPrefix(prefix, file)) | ||
continue | ||
} | ||
|
||
log.Tracef("Parsing file: %s", TrimPathPrefix(prefix, file)) | ||
tmpl, err := e.ParseFile(file) | ||
if err != nil { | ||
return err | ||
} | ||
if err = commonTemplates.Add(tmpl.Name(), tmpl); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (e *GoViewEngine) loadLayoutTemplates(layouts []string) error { | ||
dirs, err := e.DirsPath("pages") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
prefix := path.Dir(e.BaseDir) | ||
var errs []error | ||
for _, layout := range layouts { | ||
layoutKey := strings.ToLower(path.Base(layout)) | ||
|
||
for _, dir := range dirs { | ||
files, err := e.VFS.Glob(path.Join(dir, "*"+e.FileExt)) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
for _, file := range files { | ||
tmplKey := StripPathPrefixAt(filepath.ToSlash(file), "views/") | ||
tmpl := e.NewTemplate(tmplKey) | ||
tfiles := []string{layout, file} | ||
|
||
log.Tracef("Parsing files: %s", TrimPathPrefix(prefix, tfiles...)) | ||
if _, err = e.ParseFiles(tmpl, tfiles...); err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
if err = e.AddTemplate(layoutKey, tmplKey, tmpl); err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
|
||
return e.ParseErrors(errs) | ||
} | ||
|
||
func (e *GoViewEngine) loadNonLayoutTemplates(scope string) error { | ||
dirs, err := e.DirsPath(scope) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
prefix := path.Dir(e.BaseDir) | ||
var errs []error | ||
for _, dir := range dirs { | ||
files, err := e.VFS.Glob(path.Join(dir, "*"+e.FileExt)) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
for _, file := range files { | ||
tmplKey := noLayout + "-" + StripPathPrefixAt(filepath.ToSlash(file), "views/") | ||
tmpl := e.NewTemplate(tmplKey) | ||
|
||
log.Tracef("Parsing file: %s", TrimPathPrefix(prefix, file)) | ||
tstr, err := e.Open(file) | ||
if err != nil { | ||
return err | ||
} | ||
if tmpl, err = tmpl.Parse(tstr); err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
if err = e.AddTemplate(noLayout, tmplKey, tmpl); err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
} | ||
} | ||
|
||
return e.ParseErrors(errs) | ||
} | ||
|
||
func init() { | ||
_ = AddEngine("go", &GoViewEngine{}) | ||
} |
Oops, something went wrong.