Ejemplo n.º 1
0
// 首页或是列表页
func pagePosts(w http.ResponseWriter, r *http.Request) {
	info, err := getInfo()
	if err != nil {
		logs.Error("pagePosts:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	page := conv.MustInt(r.FormValue("page"), 1)
	if page == 1 {
		info.Canonical = app.URL(app.HomeURL())
	} else if page > 1 { // 为1的时候,不需要prev
		info.Canonical = app.URL(app.PostsURL(page))
		info.PrevPage = &Anchor{Title: "上一页", Link: app.PostsURL(page - 1)}
	}

	if page*opt.SidebarSize < info.PostSize {
		info.NextPage = &Anchor{Title: "下一页", Link: app.PostsURL(page + 1)}
	}

	posts, err := getPosts(page - 1)
	if err != nil {
		logs.Error("pagePosts:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	data := map[string]interface{}{
		"info":  info,
		"posts": posts,
	}
	render(w, r, "posts", data, map[string]string{"Content-Type": "text/html"})
}
Ejemplo n.º 2
0
// /tags/1.html
func pageTag(w http.ResponseWriter, r *http.Request) {
	tagName, ok := util.ParamString(w, r, "id")
	if !ok {
		return
	}
	tagName = strings.TrimSuffix(tagName, opt.Suffix)

	sql := `SELECT {id} AS {ID}, {name} AS {Name}, {title} AS {Title}, {description} AS {Description}
	FROM #tags
	WHERE {name}=?`
	rows, err := db.Query(true, sql, tagName)
	if err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	tag := &Tag{}
	if _, err = fetch.Obj(tag, rows); err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	info, err := getInfo()
	if err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	info.Canonical = app.URL(tag.Permalink())
	info.Title = tag.Title

	page := conv.MustInt(r.FormValue("page"), 1)
	if page < 1 { // 不能小于1
		page = 1
	} else if page > 1 { // 为1的时候,不需要prev
		info.PrevPage = &Anchor{Title: "上一页", Link: app.TagURL(tagName, page-1)}
	}
	if page*opt.SidebarSize < tag.Count() {
		info.NextPage = &Anchor{Title: "下一页", Link: app.TagURL(tagName, page+1)}
	}
	posts, err := getTagPosts(page-1, tag.ID)
	if err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	data := map[string]interface{}{
		"info":  info,
		"tag":   tag,
		"posts": posts,
	}
	render(w, r, "tag", data, map[string]string{"Content-Type": "text/html"})
}