// ResolveTemplate resolves a template according to the attributes passed func ResolveTemplate(templatePath string, attributes map[string]interface{}) (string, error) { templateFile, err := filepath.Abs(templatePath) if err != nil { return "", err } output, err := mustache.RenderFile(templateFile, attributes) if err != nil { return "", fmt.Errorf("error parsing template %s: %v", templatePath, err) } return output, nil }
// GetUserPage is func GetUserPage(ctx *gin.Context) { // userID := ctx.Param("id") Users := model.User{} Users.ID, _ = strconv.ParseInt(ctx.Param("id"), 10, 64) db.Find(&Users) view, err := mustache.RenderFile("view/user/Page.html.mustache", gin.H{ "id": Users.ID, "name": Users.Name, "description": Users.Description, }) if err != nil { ctx.String(500, "Internal Server Error") } ctx.Header("Content-Type", "text/html; charset=UTF-8") ctx.String(200, view) }
// GetIndex is func GetIndex(ctx *gin.Context) { defer func() { cause := recover() if cause != nil { ctx.JSON(500, gin.H{ "error": cause.(error).Error(), }) } }() view, err := mustache.RenderFile("view/index.html.mustache", gin.H{ "name": "TEST", "description": "This is test.<br/> The test is successful!!", }) if err != nil { ctx.String(500, "Internal Server Error") } ctx.Header("Content-Type", "text/html; charset=UTF-8") ctx.String(200, view) }
func Template(c *gin.Context, fp string, data ...interface{}) { html, _ := mustache.RenderFile(fp, data) c.Data(200, "text/html", []byte(html)) }