// renders / func index(ctx *web.Context) string { css, ok := ctx.Params["css"] if ok { SetCSS(ctx, css) ctx.Redirect(302, "/") return "ok" } //posts := postsForMonth(time.LocalTime()) //Db.GetLastNPosts(10) // posts := lastPosts(0xff) posts := postsForLastNDays(4) if len(posts) <= 0 { posts = lastPosts(23) } //fmt.Printf("posts: %#v\n", posts) //embedded struct - our mustache templates need a NumOfComments field to render //but we don't want to put that field into the BlogPost Struct so it won't get stored //into the DB type MyPost struct { BlogPost NumOfComments int } //posts ordered by date. this is ugly. TODO: look up if mustache hase something to handle this situation type Date struct { Date string Posts []MyPost } Db := DBGet() defer Db.Close() //loop through our posts and put them into the appropriate date structure dates := []Date{} var cur_date time.Time var date *Date for _, p := range posts { post_date := time.SecondsToLocalTime(p.Timestamp) if !(cur_date.Day == post_date.Day && cur_date.Month == post_date.Month && cur_date.Year == post_date.Year) { cur_date = *post_date dates = append(dates, Date{Date: cur_date.Format("Mon Jan _2 2006")}) date = &dates[len(dates)-1] } p.Comments, _ = Db.GetComments(p.Id) mp := MyPost{p, len(p.Comments)} date.Posts = append(date.Posts, mp) } m := map[string]interface{}{ "Dates": dates, } tmpl, _ := mustache.ParseFile("templ/index.mustache") s := tmpl.Render(&m, getCSS(ctx)) return s }
// Cache templates func getTemplate(name string) (*mustache.Template, os.Error) { tmp := server.Templates[name] if tmp == nil { tmp, err := mustache.ParseFile(getTemplateName(name)) if err != nil { return nil, err } server.Templates[name] = tmp return tmp, nil } return tmp, nil }
func (tm *TemplateMgr) VisitFile(path string, f *os.FileInfo) { temp, err := mustache.ParseFile(path) DieIf(err) if path[0] == '.' { return } if prev, ok := tm.Templates[path]; ok { log.Print(f) log.Fatal("overloading templates: ", prev) } tm.Templates[path] = temp }
// renders /post?id= func post(ctx *web.Context) string { id_s := ctx.Params["id"] id, _ := strconv.Atoi64(id_s) post := postForId(id) post.Comments, _ = Db.GetComments(post.Id) type MyPost struct { BlogPost NumOfComments int } p := MyPost{post, len(post.Comments)} tmpl, _ := mustache.ParseFile("templ/postview.mustache") s := tmpl.Render(&p, getCSS(ctx)) return s }
func render(output string, title string) string { vars := make(map[string]interface{}) vars["Body"] = output if title != "" { vars["Title"] = map[string]string{"Name": title} } p := PageModelInit() vars["SidePages"] = p.Sidebar() // Check if layout is changed if getLayoutChanged() != layoutChanged { layoutChanged = getLayoutChanged() layout, _ = mustache.ParseFile("templates/layout.mustache") } return layout.Render(vars) }
func rss(ctx *web.Context) string { Db := DBGet() defer Db.Close() posts, _ := Db.GetLastNPosts(20) //postsForMonth(time.LocalTime())// tmpl, _ := mustache.ParseFile("templ/rss.mustache") type RssItem struct { Title string Description string Link string Guid string Date string } var items []RssItem for _, post := range posts { post_date := time.SecondsToLocalTime(post.Timestamp) date := post_date.Format("Mon, 02 Jan 2006 15:04:05 -0700") title := htmlstrip(post.Content) l := len(title) if l > 64 { l = 64 } item := RssItem{ Title: string(title[0:l]), Description: post.Content, Link: fmt.Sprintf("http://fettemama.org/post?id=%d", post.Id), Guid: fmt.Sprintf("http://fettemama.org/post?id=%d", post.Id), Date: date, } items = append(items, item) } m := map[string]interface{}{"Items": items} return tmpl.Render(&m) }
func initLayout() { layoutChanged = getLayoutChanged() layout, _ = mustache.ParseFile("templates/layout.mustache") }
func init() { handle("GET", "/first/?$", first) templates["first"], _ = mustache.ParseFile("templates/first.mustache") }
func init() { handle("GET", "/study/?$", study) templates["study"], _ = mustache.ParseFile("templates/study.mustache") }
func init() { handle("GET", "/list/?$", list) templates["list"], _ = mustache.ParseFile("templates/list.mustache") }
func init() { handle("GET", "/add/?$", add) handle("POST", "/add/?$", add) templates["add"], _ = mustache.ParseFile("templates/add.mustache") }
//renders /month?m=<>&y= //if m and/or y are not filled the value of Today will be used func month(ctx *web.Context) string { Db := DBGet() defer Db.Close() mon := ctx.Params["m"] yr := ctx.Params["y"] d := time.LocalTime() if len(mon) > 0 { d.Month, _ = strconv.Atoi(mon) } if len(yr) > 0 { d.Year, _ = strconv.Atoi64(yr) } posts := postsForMonth(d) //Db.GetLastNPosts(10) //fmt.Printf("posts: %#v\n", posts) //embedded struct - our mustache templates need a NumOfComments field to render //but we don't want to put that field into the BlogPost Struct so it won't get stored //into the DB type MyPost struct { BlogPost NumOfComments int } //posts ordered by date. this is ugly. TODO: look up if mustache hase something to handle this situation type Date struct { Date string Posts []MyPost } //loop through our posts and put them into the appropriate date structure dates := []Date{} var cur_date time.Time var date *Date for _, p := range posts { post_date := time.SecondsToLocalTime(p.Timestamp) if !(cur_date.Day == post_date.Day && cur_date.Month == post_date.Month && cur_date.Year == post_date.Year) { cur_date = *post_date dates = append(dates, Date{Date: cur_date.Format("Mon Jan _2 2006")}) date = &dates[len(dates)-1] } p.Comments, _ = Db.GetComments(p.Id) mp := MyPost{p, len(p.Comments)} date.Posts = append(date.Posts, mp) } //create PrevMonth pmon := *d pmon.Month-- if pmon.Month <= 0 { pmon.Year-- pmon.Month = 12 } //fill map m := map[string]interface{}{ "Dates": dates, "PrevMonth": pmon, } tmpl, _ := mustache.ParseFile("templ/month.mustache") s := tmpl.Render(&m, getCSS(ctx)) return s }
func init() { http.HandleFunc("/", routeHandler) templates["404"], _ = mustache.ParseFile("templates/404.mustache") }