// InitAssetsTemplates initializes the router to use either a ricebox or the // filesystem in case the ricebox couldn't be found. func InitAssetsTemplates(r *gin.Engine, tbox, abox *rice.Box, verbose bool, names ...string) error { var err error if tbox != nil { mt := multitemplate.New() var tmpl string var message *template.Template for _, x := range names { if tmpl, err = tbox.String(x); err != nil { return err } if message, err = template.New(x).Parse(tmpl); err != nil { return err } mt.Add(x, message) } logger.Debug("server", "Loaded templates from \"templates\" box") r.HTMLRender = mt } else { r.LoadHTMLGlob("templates/*") logger.Debug("server", "Loaded templates from disk") } if abox != nil { r.StaticFS("/static", abox.HTTPBox()) logger.Debug("server", "Loaded assets from \"assets\" box") } else { r.Static("/static", "assets") logger.Debug("server", "Loaded assets from disk") } return nil }
func loadTemplatesDefault(templatesDir string) multitemplate.Render { r := multitemplate.New() layoutDir := templatesDir + "/layouts/" layouts, err := filepath.Glob(layoutDir + "*/*" + conf.TMPL_SUFFIX) if err != nil { panic(err.Error()) } includeDir := templatesDir + "/includes/" includes, err := filepath.Glob(includeDir + "*" + conf.TMPL_SUFFIX) if err != nil { panic(err.Error()) } // Generate our templates map from our layouts/ and includes/ directories for _, layout := range layouts { files := append(includes, layout) tmpl := template.Must(template.ParseFiles(files...)) tmplName := strings.TrimPrefix(layout, layoutDir) tmplName = strings.TrimSuffix(tmplName, conf.TMPL_SUFFIX) log.DebugPrint("Tmpl add " + tmplName) r.Add(tmplName, tmpl) } return r }
func loadTemplatesBindata(templatesDir string) multitemplate.Render { r := multitemplate.New() layoutDir := templatesDir + "/layouts" layoutDirs, err := templates.AssetDir(layoutDir) if err != nil { panic(err.Error()) } var layouts []string for _, dir := range layoutDirs { files, err := templates.AssetDir(layoutDir + "/" + dir) if err != nil { panic(err.Error()) } // 过滤非.tmpl后缀模板 layoutFiels, err := tmplsFilter(files, layoutDir+"/"+dir) if err != nil { panic(err.Error()) } layouts = append(layouts, layoutFiels...) } includeDir := templatesDir + "/includes" includeFiels, err := templates.AssetDir(includeDir) if err != nil { panic(err.Error()) } // 过滤非.tmpl后缀模板 includes, err := tmplsFilter(includeFiels, includeDir) if err != nil { panic(err.Error()) } // Generate our templates map from our layouts/ and includes/ directories for _, layout := range layouts { files := append(includes, layout) tmpl := template.Must(parseBindataFiles(files...)) tmplName := strings.TrimPrefix(layout, layoutDir+"/") tmplName = strings.TrimSuffix(tmplName, conf.TMPL_SUFFIX) log.DebugPrint("Tmpl add " + tmplName) r.Add(tmplName, tmpl) } return r }
/* * Main function */ func main() { //fmt.Printf("Register function . Sqrt1(4) = %v\n", register.Sqrt1(4)) r := gin.New() // r := gin.Default() store := sessions.NewCookieStore([]byte("myappsecret")) r.Use(sessions.Sessions("mygroups", store)) r.Use(Is_authorised()) //r.LoadHTMLGlob("templates/*") templates := multitemplate.New() templates.AddFromFiles("home", "home.html", "header.html", "footer.html") templates.AddFromFiles("register", "register.html", "header.html", "footer.html") r.HTMLRender = templates //fmt.Println(register.Sqrt(2)) //r.HTMLRender = loadTemplates("./templates") r.StaticFS("/css/", http.Dir("css")) r.StaticFS("/images/", http.Dir("images")) /* r.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "home", gin.H{ "Title": "Login Here", }, ) }) */ r.GET("/", register.HomeHandler) r.POST("/", register.LoginHandler) r.POST("/register", register.RegisterHandler) r.GET("/register", func(c *gin.Context) { c.HTML(http.StatusOK, "register", gin.H{ "Title": "New Registration", }, ) }) r.Run(":8090") }
// renderer is a function, where all needed templates are configured func renderer() multitemplate.Render { var layoutsPath = "views/layouts/" var pagesPath = "views/pages/" r := multitemplate.New() // Index page template r.AddFromFiles("index", layoutsPath+"application.tmpl", layoutsPath+"header.tmpl", layoutsPath+"footer.tmpl", layoutsPath+"share.tmpl", layoutsPath+"magicbox.tmpl", pagesPath+"index.tmpl") // Result page template r.AddFromFiles("result", layoutsPath+"application.tmpl", layoutsPath+"header.tmpl", layoutsPath+"footer.tmpl", layoutsPath+"share.tmpl", layoutsPath+"magicbox.tmpl", pagesPath+"result.tmpl") // About page template r.AddFromFiles("about", layoutsPath+"application.tmpl", layoutsPath+"header.tmpl", layoutsPath+"footer.tmpl", layoutsPath+"share.tmpl", pagesPath+"about.tmpl") // Tips page template r.AddFromFiles("tips", layoutsPath+"application.tmpl", layoutsPath+"header.tmpl", layoutsPath+"footer.tmpl", layoutsPath+"share.tmpl", pagesPath+"tips.tmpl") return r }
func createCustomRender() multitemplate.Render { render := multitemplate.New() render.AddFromFiles("satellite", "web/templates/default.html", "web/templates/satellite.html") render.AddFromFiles("error", "web/templates/default.html", "web/templates/error.html") return render }