func adminGet(ctx *web.Context) string { if !checkGodLevel(ctx) { return mustache.RenderFile("templ/admin_login.mustache") } return mustache.RenderFile("templ/admin_post.mustache") }
func renderDefault(context gon.WebContext, ret []reflect.Value, controllerName string, actionName string) { if model, ok := ret[0].Interface().(mv.Model); ok { context.WriteString(mustache.RenderFile(APP_VIEW_PATH+controllerName+"/"+actionName+".m", model)) } else if view, ok := ret[0].Interface().(mv.View); ok { actionName = view.String() context.WriteString(mustache.RenderFile(APP_VIEW_PATH + controllerName + "/" + actionName + ".m")) } }
func Get(ctx *web.Context, val string) { v := strings.Split(val, "/", 2) controllerName := "" actionName := "" if len(v) == 2 { controllerName, actionName = v[0], v[1] } else if len(v) == 1 { controllerName = v[0] actionName = "index" } if conType, ok := C.Controllers[controllerName]; ok { conTypePtr := reflect.PtrTo(conType) actionMethName := strings.ToUpper(string(actionName[0:1])) + actionName[1:] var actionMeth reflect.Method found := false for i := 0; i < conTypePtr.NumMethod(); i++ { if conTypePtr.Method(i).Name == actionMethName { actionMeth = conTypePtr.Method(i) found = true break } } if !found { return } conValue := reflect.New(conType) conIndirect := reflect.Indirect(conValue) // Inject Params conIndirect.FieldByName("Params").Set(reflect.ValueOf(ctx.Request.Params)) // Inject beans for beanName, setterFunc := range bean.Registry() { if _, ok := conType.FieldByName(beanName); ok { if f := conIndirect.FieldByName(beanName); f.IsValid() { f.Set(reflect.ValueOf(setterFunc())) } } } action := actionMeth.Func ret := action.Call([]reflect.Value{conValue}) if len(ret) == 2 { m := ret[0].Interface().(mv.Model) v := ret[1].Interface().(mv.View) controllerName = v.String() ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/index.m", m)) } else if len(ret) == 1 { m := ret[0].Interface().(mv.Model) ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/"+actionName+".m", m)) } } return }
func editGet(ctx *web.Context) string { if !checkGodLevel(ctx) { return mustache.RenderFile("templ/admin_login.mustache") } id, _ := strconv.Atoi64(ctx.Params["id"]) post, err := Db.GetPost(id) if err != nil { return "couldn't load post with given id!" } return mustache.RenderFile("templ/admin_edit.mustache", &post) }
func adminGet(ctx *web.Context) string { if !checkGodLevel(ctx) { return mustache.RenderFile("templ/admin_login.mustache") } Db := DBGet() defer Db.Close() posts, _ := Db.GetLastNPosts(256) x := map[interface{}]interface{}{ "Posts": posts, } return mustache.RenderFile("templ/admin_post.mustache", &x) }
func (c *Index) RSS(ctx *web.Context) string { p := PostModelInit() results := p.RSS() ctx.ContentType("xml") return mustache.RenderFile("templates/rss.mustache", map[string][]map[string]string{"posts": results}) }
func (c *Index) Index() string { p := PostModelInit() results := p.FrontPage() output := mustache.RenderFile("templates/post.mustache", map[string][]map[string]string{"posts": results}) return render(output, "") }
func (c *Index) ReadPost(ctx *web.Context, postId string) string { p := PostModelInit() result := p.RenderPost(postId) viewVars := make(map[string]interface{}) viewVars["Title"] = result.Title viewVars["Content"] = result.Content viewVars["Date"] = time.Unix(result.Created, 0).Format(blogConfig.Get("dateFormat")) viewVars["Id"] = objectIdHex(result.Id.String()) // To be used within the {{Comments}} blog viewVars["PostId"] = objectIdHex(result.Id.String()) if result.Status == 0 { sessionH := session.Start(ctx, h) defer sessionH.Save() if sessionH.Data["logged"] == nil { ctx.Redirect(302, "/") return "" } } if blogConfig.Get("enableComment") != "" { viewVars["EnableComment"] = true } else { viewVars["EnableComment"] = false } // Render comments comments := make([]map[string]string, 0) for i, v := range result.Comments { comments = append(comments, map[string]string{ "Number": strconv.Itoa(i + 1), "Date": time.Unix(v.Created, 0).Format(blogConfig.Get("dateFormat")), "Id": v.Id[0:9], "RealId": v.Id, "Content": v.Content, "Author": v.Author}) } viewVars["Comments"] = comments if next, exists := p.GetNextId(objectIdHex(result.Id.String())); exists { viewVars["Next"] = next } if last, exists := p.GetLastId(objectIdHex(result.Id.String())); exists { viewVars["Last"] = last } sessionH := session.Start(ctx, h) defer sessionH.Save() viewVars["Admin"] = false if sessionH.Data["logged"] != nil { viewVars["Admin"] = true } output := mustache.RenderFile("templates/view-post.mustache", viewVars) return render(output, result.Title) }
func editGet(ctx *web.Context) string { if !checkGodLevel(ctx) { return mustache.RenderFile("templ/admin_login.mustache") } Db := DBGet() defer Db.Close() id, _ := strconv.Atoi64(ctx.Params["id"]) post, err := Db.GetPost(id) if err != nil { return "couldn't load post with given id!" } posts, _ := Db.GetLastNPosts(256) x := map[interface{}]interface{}{ "Posts": posts, } return mustache.RenderFile("templ/admin_edit.mustache", &post, &x) }
func (c *Index) ReadPage(pageSlug string) string { p := PageModelInit() result := p.GetBySlug(pageSlug) viewVars := make(map[string]string) viewVars["Title"] = result.Title viewVars["Content"] = result.Content viewVars["Date"] = time.Unix(result.Created, 0).Format(blogConfig.Get("dateFormat")) output := mustache.RenderFile("templates/view-page.mustache", viewVars) return render(output, result.Title) }
func (c *Index) ListPosts(ctx *web.Context) string { page := 1 if temp, exists := ctx.Params["page"]; exists { page, _ = strconv.Atoi(temp) } p := PostModelInit() results := p.PostListing(page) totPages := p.TotalPages() output := mustache.RenderFile("templates/post-listing.mustache", map[string]interface{}{"Posts": results, "Pagination": pagination(page, totPages)}) return render(output, "Post Listing") }
func (this *Action) Render(context interface{}) string { log.Stdoutf("Rendering %s", this.template) filename := GetConfig("templates") + "/" + this.template + ".mustache" output, err := mustache.RenderFile(filename, context) if err != nil { log.Stderrf("Error on rendering %s", filename, err) return "" // TODO error page } if this.layout != nil { output = this.layout.Render(map[string]string{"yield": output}) } return output }
func Get(ctx *web.Context, val string) { v := strings.Split(val, "/", 2) controllerName, actionName := v[0], v[1] if action, ok := C.Controllers[controllerName+"/"+actionName]; ok { for beanName, setterFunc := range bean.Registry { if target, ok := C.Injectables[controllerName+"."+beanName]; ok { v := reflect.ValueOf(setterFunc()) reflect.Indirect(target).Set(reflect.Indirect(v)) } } ret := action.Call([]reflect.Value{}) if len(ret) == 2 { m := ret[0].Interface().(mv.Model) v := ret[1].Interface().(mv.View) controllerName = v.String() ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/index.m", m)) } else if len(ret) == 1 { m := ret[0].Interface().(mv.Model) ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/"+actionName+".m", m)) } } return }
func main() { var rendered_content string var template []uint8 var error os.Error var inode *os.Dir flag.Usage = usage flag.Parse() if flag.NArg() == 0 { fmt.Fprintf(os.Stderr, "no template rules defined\n") os.Exit(ERROR) } rules := make(Dictionary) for i := 0; i < flag.NArg(); i++ { rules.define(flag.Args()[i]) } if *input_file == "" { if template, error = ioutil.ReadAll(os.Stdin); error == nil { rendered_content, error = mustache.Render(string(template), rules) } } else { switch inode, error = os.Stat(*input_file); { case error != nil: case inode.IsRegular(): rendered_content, error = mustache.RenderFile(*input_file, rules) default: } } if error != nil { os.Exit(ERROR) } else { if *write { error = ioutil.WriteFile(*input_file, []byte(rendered_content), 0) } else { _, error = os.Stdout.Write([]byte(rendered_content)) } if error != nil { os.Exit(ERROR) } } os.Exit(0) }
/*just prints a basic form to receive a URL*/ func index(ctx *web.Context) { ctx.WriteString(mustache.RenderFile("index.mustache")) }
func renderWithActionName(context gon.WebContext, ret []reflect.Value, controllerName string) { model := ret[0].Interface().(mv.Model) view := ret[1].Interface().(mv.View) actionName := view.String() context.WriteString(mustache.RenderFile(APP_VIEW_PATH+controllerName+"/"+actionName+".m", model)) }
func renderRoot(context gon.WebContext) { context.WriteString(mustache.RenderFile(APP_VIEW_PATH + "main.m")) }
func printError(ctx *web.Context, error string) { /*this is a 500 error condition*/ ctx.StartResponse(500) /*print boilerplate error page with passed in message*/ ctx.WriteString(mustache.RenderFile("error.mustache", map[string]string{"error": error})) }
func hellos() string { filename := path.Join(path.Join(os.Getenv("PWD"), "tmpl"), "hellos.mustache") output := mustache.RenderFile(filename) return output }
func render(view string, context ...interface{}) string { return mustache.RenderFile("views/"+view+".mustache", context...) }
/*shows the "shortened" URL to the user*/ func show(ctx *web.Context, key string) { ctx.WriteString(mustache.RenderFile("show.mustache", map[string]string{"key": key})) }