Skip to content

Commit

Permalink
#200 Moved view pkg into main repo
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Aug 18, 2018
2 parents 7771a6d + 6e08de1 commit 0c32e42
Show file tree
Hide file tree
Showing 30 changed files with 1,222 additions and 0 deletions.
52 changes: 52 additions & 0 deletions view/funcs.go
@@ -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())
}
196 changes: 196 additions & 0 deletions view/go_engine.go
@@ -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{})
}

0 comments on commit 0c32e42

Please sign in to comment.