예제 #1
0
func ArticleHandler(ctx *app.Context) {
	slug := ctx.IndexValue(0)
	var art *article.Article
	articles := getArticles(ctx)
	for _, v := range articles {
		if v.Slug() == slug {
			art = v
			break
		}
	}
	if art == nil {
		for _, v := range articles {
			for _, s := range v.AllSlugs() {
				if s == slug {
					ctx.MustRedirectReverse(true, ctx.HandlerName(), s)
					return
				}
			}
		}
		ctx.NotFound("article not found")
		return
	}
	fs := vfs.Memory()
	filename := path.Base(art.Filename)
	if filename == "" {
		filename = "article.html"
	}
	if err := vfs.WriteFile(fs, filename, art.Text, 0644); err != nil {
		panic(err)
	}
	log.Debugf("loading article %s", articleId(art))
	tmpl, err := app.LoadTemplate(ctx.App(), fs, nil, filename)
	if err != nil {
		panic(err)
	}
	var buf bytes.Buffer
	if err := tmpl.ExecuteTo(&buf, ctx, nil); err != nil {
		panic(err)
	}
	body := buf.String()
	data := map[string]interface{}{
		"Article": art,
		"Title":   art.Title(),
		"Body":    template.HTML(body),
	}
	ctx.MustExecute("article.html", data)
}
예제 #2
0
파일: pager.go 프로젝트: rainycape/gondola
// NewPager returns a new *Pager instance. Note that the
// Context must satisfy the conditions mentioned in the
// Pager type documentation. Otherwise an error will be
// returned.
func NewPager(ctx *app.Context) (*Pager, error) {
	name := ctx.HandlerName()
	if name == "" {
		return nil, errors.New("can't generate a pager from an unnamed handler")
	}
	names := ctx.Provider().ParamNames()
	if len(names) == 0 {
		return nil, errors.New("can't generate a pager from a handler with no parameters, must have a page parameter")
	}
	if names[len(names)-1] != "page" {
		return nil, fmt.Errorf("last named parameter is %q, not page", names[len(names)-1])
	}
	params := make([]interface{}, ctx.Count()-1)
	for ii := 0; ii < ctx.Count()-1; ii++ {
		params[ii] = ctx.IndexValue(ii)
	}
	return &Pager{
		Name:       name,
		Parameters: params,
		Ctx:        ctx,
	}, nil
}