Example #1
0
func PostBySlug(ctx *context.Context) http.HandlerFunc {

	return func(w http.ResponseWriter, r *http.Request) {

		cashed, exist := ctx.Cashe[r.RequestURI]
		if exist {
			fmt.Fprint(w, cashed)
			return
		}

		vars := mux.Vars(r)
		post_slug := vars["slug"]

		var post m.Post

		err := post.GetBySlug(ctx.Db, post_slug)
		if err != nil {
			http.NotFound(w, r)
			return
		}

		funcMap := template.FuncMap{
			"tags": func(s string) string {
				s = strings.Trim(s, "{")
				s = strings.Trim(s, "}")
				return s
			},
			"date2german": func(t time.Time) string {
				year, month, day := t.Date()
				c := strconv.Itoa
				z := func(d int) string {
					if d < 10 {
						return "0" + c(d)
					} else {
						return c(d)
					}
				}
				return z(day) + "." + z(int(month)) + "." + z(year)
			},
		}

		var b bytes.Buffer

		//t := template.Must(template.New("post.html").Funcs(funcMap).ParseFiles(ctx.Conf.Templates.Post...))
		t := template.Must(template.New(path.Base(ctx.Conf.Templates.Post[0])).Funcs(funcMap).ParseFiles(ctx.Conf.Templates.Post...))
		if err := t.Execute(&b, map[string]interface{}{
			"post": post,
		}); err != nil {
			fmt.Println(err.Error())
		}

		ctx.Cashe[r.RequestURI] = b.String()
		fmt.Fprint(w, ctx.Cashe[r.RequestURI])
	}
}
Example #2
0
func Post(ctx *context.Context) http.HandlerFunc {

	return func(w http.ResponseWriter, r *http.Request) {

		vars := mux.Vars(r)
		post_id, _ := strconv.Atoi(vars["id"])

		var post m.Post

		err := post.GetById(ctx.Db, post_id)
		if err != nil {
			http.NotFound(w, r)
			return
		}

		funcMap := template.FuncMap{
			"tags": func(s string) string {
				s = strings.Trim(s, "{")
				s = strings.Trim(s, "}")
				return s
			},
			"date2german": func(t time.Time) string {
				year, month, day := t.Date()
				c := strconv.Itoa
				z := func(d int) string {
					if d < 10 {
						return "0" + c(d)
					} else {
						return c(d)
					}
				}
				return z(day) + "." + z(int(month)) + "." + z(year)
			},
		}

		//t := template.Must(template.New("post.html").Funcs(funcMap).ParseFiles(ctx.Conf.Templates.Post...))
		t := template.Must(template.New(path.Base(ctx.Conf.Templates.Post[0])).Funcs(funcMap).ParseFiles(ctx.Conf.Templates.Post...))
		if err := t.Execute(w, map[string]interface{}{
			"post": post,
		}); err != nil {
			fmt.Println(err.Error())
		}

	}
}