Skip to content

Commit

Permalink
🐛 fix third-party template loading
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Nov 29, 2023
1 parent efa9a30 commit 6a25f39
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions cmd/dashboard/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
Expand All @@ -16,7 +18,6 @@ import (
"github.com/nicksnyder/go-i18n/v2/i18n"

"github.com/naiba/nezha/pkg/mygin"
"github.com/naiba/nezha/pkg/utils"
"github.com/naiba/nezha/resource"
"github.com/naiba/nezha/service/singleton"
)
Expand All @@ -30,12 +31,7 @@ func ServeWeb(port uint) *http.Server {
if err != nil {
panic(err)
}
if utils.IsFileExists("resource/template") {
tmpl, err = tmpl.ParseGlob("resource/template/**/*.html")
if err != nil {
panic(err)
}
}
tmpl = loadThirdPartyTemplates(tmpl)
r.SetHTMLTemplate(tmpl)
if singleton.Conf.Debug {
gin.SetMode(gin.DebugMode)
Expand Down Expand Up @@ -87,6 +83,28 @@ func routers(r *gin.Engine) {
}
}

func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
var ret = tmpl
themes, err := os.ReadDir("resource/template")
if err != nil {
log.Printf("NEZHA>> Error reading themes folder: %v", err)
return ret
}
for _, theme := range themes {
if !theme.IsDir() {
continue
}
// load templates
t, err := ret.ParseGlob(fmt.Sprintf("resource/template/%s/*.html", theme.Name()))
if err != nil {
log.Printf("NEZHA>> Error parsing templates %s error: %v", theme.Name(), err)
continue
}
ret = t
}
return ret
}

var funcMap = template.FuncMap{
"tr": func(id string, dataAndCount ...interface{}) string {
conf := i18n.LocalizeConfig{
Expand Down

0 comments on commit 6a25f39

Please sign in to comment.