Fixed custom templates for static builds (#1087)

tokarchuk/v1.17
Thomas Boerger 8 years ago committed by Lunny Xiao
parent d21d5fd736
commit db6777d369
  1. 2
      modules/templates/helper.go
  2. 102
      modules/templates/static.go

@ -49,7 +49,7 @@ func NewFuncMap() []template.FuncMap {
return setting.AppVer return setting.AppVer
}, },
"AppBuiltWith": func() string { "AppBuiltWith": func() string {
return setting.AppBuiltWith return setting.AppBuiltWith
}, },
"AppDomain": func() string { "AppDomain": func() string {
return setting.Domain return setting.Domain

@ -7,7 +7,10 @@
package templates package templates
import ( import (
"bytes"
"fmt"
"html/template" "html/template"
"io"
"io/ioutil" "io/ioutil"
"path" "path"
"strings" "strings"
@ -15,7 +18,6 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/go-macaron/bindata"
"gopkg.in/macaron.v1" "gopkg.in/macaron.v1"
) )
@ -23,22 +25,94 @@ var (
templates = template.New("") templates = template.New("")
) )
type templateFileSystem struct {
files []macaron.TemplateFile
}
func (templates templateFileSystem) ListFiles() []macaron.TemplateFile {
return templates.files
}
func (templates templateFileSystem) Get(name string) (io.Reader, error) {
for i := range templates.files {
if templates.files[i].Name()+templates.files[i].Ext() == name {
return bytes.NewReader(templates.files[i].Data()), nil
}
}
return nil, fmt.Errorf("file '%s' not found", name)
}
// Renderer implements the macaron handler for serving the templates. // Renderer implements the macaron handler for serving the templates.
func Renderer() macaron.Handler { func Renderer() macaron.Handler {
fs := templateFileSystem{}
fs.files = make([]macaron.TemplateFile, 0, 10)
for _, assetPath := range AssetNames() {
if strings.HasPrefix(assetPath, "mail/") {
continue
}
if !strings.HasSuffix(assetPath, ".tmpl") {
continue
}
content, err := Asset(assetPath)
if err != nil {
log.Warn("Failed to read embedded %s template. %v", assetPath, err)
continue
}
fs.files = append(fs.files, macaron.NewTplFile(
strings.TrimSuffix(
assetPath,
".tmpl",
),
content,
".tmpl",
))
}
customDir := path.Join(setting.CustomPath, "templates")
if com.IsDir(customDir) {
files, err := com.StatDir(customDir)
if err != nil {
log.Warn("Failed to read %s templates dir. %v", customDir, err)
} else {
for _, filePath := range files {
if strings.HasPrefix(filePath, "mail/") {
continue
}
if !strings.HasSuffix(filePath, ".tmpl") {
continue
}
content, err := ioutil.ReadFile(path.Join(customDir, filePath))
if err != nil {
log.Warn("Failed to read custom %s template. %v", filePath, err)
continue
}
fs.files = append(fs.files, macaron.NewTplFile(
strings.TrimSuffix(
filePath,
".tmpl",
),
content,
".tmpl",
))
}
}
}
return macaron.Renderer(macaron.RenderOptions{ return macaron.Renderer(macaron.RenderOptions{
Funcs: NewFuncMap(), Funcs: NewFuncMap(),
AppendDirectories: []string{ TemplateFileSystem: fs,
path.Join(setting.CustomPath, "templates"),
},
TemplateFileSystem: bindata.Templates(
bindata.Options{
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo,
AssetNames: AssetNames,
Prefix: "",
},
),
}) })
} }

Loading…
Cancel
Save