golint happy with template

This commit is contained in:
astaxie 2015-09-08 23:29:58 +08:00
parent 8615f875f8
commit 44bd3beb5e
2 changed files with 19 additions and 23 deletions

View File

@ -28,17 +28,14 @@ import (
) )
var ( var (
beegoTplFuncMap template.FuncMap beegoTplFuncMap = make(template.FuncMap)
// beego template caching map and supported template file extensions. // BeeTemplates caching map and supported template file extensions.
BeeTemplates map[string]*template.Template BeeTemplates = make(map[string]*template.Template)
BeeTemplateExt []string // BeeTemplateExt stores the template extention which will build
BeeTemplateExt = []string{"tpl", "html"}
) )
func init() { func init() {
BeeTemplates = make(map[string]*template.Template)
beegoTplFuncMap = make(template.FuncMap)
BeeTemplateExt = make([]string, 0)
BeeTemplateExt = append(BeeTemplateExt, "tpl", "html")
beegoTplFuncMap["dateformat"] = DateFormat beegoTplFuncMap["dateformat"] = DateFormat
beegoTplFuncMap["date"] = Date beegoTplFuncMap["date"] = Date
beegoTplFuncMap["compare"] = Compare beegoTplFuncMap["compare"] = Compare
@ -79,7 +76,7 @@ type templatefile struct {
files map[string][]string files map[string][]string
} }
func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { func (tf *templatefile) visit(paths string, f os.FileInfo, err error) error {
if f == nil { if f == nil {
return err return err
} }
@ -92,21 +89,21 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error {
replace := strings.NewReplacer("\\", "/") replace := strings.NewReplacer("\\", "/")
a := []byte(paths) a := []byte(paths)
a = a[len([]byte(self.root)):] a = a[len([]byte(tf.root)):]
file := strings.TrimLeft(replace.Replace(string(a)), "/") file := strings.TrimLeft(replace.Replace(string(a)), "/")
subdir := filepath.Dir(file) subdir := filepath.Dir(file)
if _, ok := self.files[subdir]; ok { if _, ok := tf.files[subdir]; ok {
self.files[subdir] = append(self.files[subdir], file) tf.files[subdir] = append(tf.files[subdir], file)
} else { } else {
m := make([]string, 1) m := make([]string, 1)
m[0] = file m[0] = file
self.files[subdir] = m tf.files[subdir] = m
} }
return nil return nil
} }
// return this path contains supported template extension of beego or not. // HasTemplateExt return this path contains supported template extension of beego or not.
func HasTemplateExt(paths string) bool { func HasTemplateExt(paths string) bool {
for _, v := range BeeTemplateExt { for _, v := range BeeTemplateExt {
if strings.HasSuffix(paths, "."+v) { if strings.HasSuffix(paths, "."+v) {
@ -116,7 +113,7 @@ func HasTemplateExt(paths string) bool {
return false return false
} }
// add new extension for template. // AddTemplateExt add new extension for template.
func AddTemplateExt(ext string) { func AddTemplateExt(ext string) {
for _, v := range BeeTemplateExt { for _, v := range BeeTemplateExt {
if v == ext { if v == ext {
@ -126,15 +123,14 @@ func AddTemplateExt(ext string) {
BeeTemplateExt = append(BeeTemplateExt, ext) BeeTemplateExt = append(BeeTemplateExt, ext)
} }
// build all template files in a directory. // BuildTemplate will build all template files in a directory.
// it makes beego can render any template file in view directory. // it makes beego can render any template file in view directory.
func BuildTemplate(dir string, files ...string) error { func BuildTemplate(dir string, files ...string) error {
if _, err := os.Stat(dir); err != nil { if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
} else {
return errors.New("dir open err")
} }
return errors.New("dir open err")
} }
self := &templatefile{ self := &templatefile{
root: dir, root: dir,

View File

@ -20,11 +20,11 @@ import (
"testing" "testing"
) )
var header string = `{{define "header"}} var header = `{{define "header"}}
<h1>Hello, astaxie!</h1> <h1>Hello, astaxie!</h1>
{{end}}` {{end}}`
var index string = `<!DOCTYPE html> var index = `<!DOCTYPE html>
<html> <html>
<head> <head>
<title>beego welcome template</title> <title>beego welcome template</title>
@ -37,7 +37,7 @@ var index string = `<!DOCTYPE html>
</html> </html>
` `
var block string = `{{define "block"}} var block = `{{define "block"}}
<h1>Hello, blocks!</h1> <h1>Hello, blocks!</h1>
{{end}}` {{end}}`
@ -82,7 +82,7 @@ func TestTemplate(t *testing.T) {
os.RemoveAll(dir) os.RemoveAll(dir)
} }
var menu string = `<div class="menu"> var menu = `<div class="menu">
<ul> <ul>
<li>menu1</li> <li>menu1</li>
<li>menu2</li> <li>menu2</li>
@ -90,7 +90,7 @@ var menu string = `<div class="menu">
</ul> </ul>
</div> </div>
` `
var user string = `<!DOCTYPE html> var user = `<!DOCTYPE html>
<html> <html>
<head> <head>
<title>beego welcome template</title> <title>beego welcome template</title>