コード例 #1
0
ファイル: handlebars.go プロジェクト: sshitaime/journey
func pagesFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	var count int64
	var err error
	if values.CurrentTemplate == 0 { // index
		count = values.Blog.PostCount
	} else if values.CurrentTemplate == 2 { // tag
		count, err = database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
		if err != nil {
			log.Println("Couldn't get number of posts for tag", err.Error())
			return []byte{}
		}
	} else if values.CurrentTemplate == 3 { // author
		count, err = database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
		if err != nil {
			log.Println("Couldn't get number of posts for author", err.Error())
			return []byte{}
		}
	}
	maxPages := positiveCeilingInt64(float64(count) / float64(values.Blog.PostsPerPage))
	// Output at least 1 (even if there are no posts in the database)
	if maxPages == 0 {
		maxPages = 1
	}
	return []byte(strconv.FormatInt(maxPages, 10))
}
コード例 #2
0
ファイル: handlebars.go プロジェクト: sshitaime/journey
func paginationDotTotalFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	if values.CurrentTemplate == 0 { // index
		return []byte(strconv.FormatInt(values.Blog.PostCount, 10))
	} else if values.CurrentTemplate == 3 { // author
		count, err := database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
		if err != nil {
			log.Println("Couldn't get number of posts", err.Error())
			return []byte{}
		}
		return []byte(strconv.FormatInt(count, 10))
	} else if values.CurrentTemplate == 2 { // tag
		count, err := database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
		if err != nil {
			log.Println("Couldn't get number of posts", err.Error())
			return []byte{}
		}
		return []byte(strconv.FormatInt(count, 10))
	}
	return []byte{}
}
コード例 #3
0
ファイル: handlebars.go プロジェクト: sshitaime/journey
func nextFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	var count int64
	var err error
	if values.CurrentTemplate == 0 { // index
		count = values.Blog.PostCount
	} else if values.CurrentTemplate == 2 { // tag
		count, err = database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
		if err != nil {
			log.Println("Couldn't get number of posts for tag", err.Error())
			return []byte{}
		}
	} else if values.CurrentTemplate == 3 { // author
		count, err = database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
		if err != nil {
			log.Println("Couldn't get number of posts for author", err.Error())
			return []byte{}
		}
	}
	maxPages := positiveCeilingInt64(float64(count) / float64(values.Blog.PostsPerPage))
	if int64(values.CurrentIndexPage) < maxPages {
		return []byte{1}
	}
	return []byte{}
}
コード例 #4
0
ファイル: handlebars.go プロジェクト: sshitaime/journey
func page_urlFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	if len(helper.Arguments) != 0 {
		if helper.Arguments[0].Name == "prev" || helper.Arguments[0].Name == "pagination.prev" {
			if values.CurrentIndexPage > 1 {
				var buffer bytes.Buffer
				if values.CurrentIndexPage == 2 {
					if values.CurrentTemplate == 3 { // author
						buffer.WriteString("/author/")
						//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
						buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
					} else if values.CurrentTemplate == 2 { // tag
						buffer.WriteString("/tag/")
						//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
						buffer.WriteString(values.CurrentTag.Slug)
					}
					buffer.WriteString("/")
				} else {
					if values.CurrentTemplate == 3 { // author
						buffer.WriteString("/author/")
						//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
						buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
					} else if values.CurrentTemplate == 2 { // tag
						buffer.WriteString("/tag/")
						//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
						buffer.WriteString(values.CurrentTag.Slug)
					}
					buffer.WriteString("/page/")
					buffer.WriteString(strconv.Itoa(values.CurrentIndexPage - 1))
					buffer.WriteString("/")
				}
				return buffer.Bytes()
			}
		} else if helper.Arguments[0].Name == "next" || helper.Arguments[0].Name == "pagination.next" {
			var count int64
			var err error
			if values.CurrentTemplate == 0 { // index
				count = values.Blog.PostCount
			} else if values.CurrentTemplate == 2 { // tag
				count, err = database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
				if err != nil {
					log.Println("Couldn't get number of posts for tag", err.Error())
					return []byte{}
				}
			} else if values.CurrentTemplate == 3 { // author
				count, err = database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
				if err != nil {
					log.Println("Couldn't get number of posts for author", err.Error())
					return []byte{}
				}
			}
			maxPages := positiveCeilingInt64(float64(count) / float64(values.Blog.PostsPerPage))
			if int64(values.CurrentIndexPage) < maxPages {
				var buffer bytes.Buffer
				if values.CurrentTemplate == 3 { // author
					buffer.WriteString("/author/")
					// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
					buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
				} else if values.CurrentTemplate == 2 { // tag
					buffer.WriteString("/tag/")
					// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
					buffer.WriteString(values.CurrentTag.Slug)
				}
				buffer.WriteString("/page/")
				buffer.WriteString(strconv.Itoa(values.CurrentIndexPage + 1))
				buffer.WriteString("/")
				return buffer.Bytes()
			}
		}
	}
	return []byte{}
}