int __tagcat(struct req *req, const char *tagcat, int page, char *tmpl, bool istag) { const unsigned int posts_per_page = req->opts.index_stories; struct post *posts[posts_per_page]; struct str *tag; int nposts; if (!tagcat) return R404(req, NULL); tag = STR_DUP(tagcat); req_head(req, "Content-Type", "text/html"); page = MAX(page, 0); __store_title(&req->vars, tagcat); __store_pages(&req->vars, page); __store_tag(&req->vars, tagcat); sidebar(req); vars_scope_push(&req->vars); nposts = index_get_posts(posts, tag, istag, NULL, NULL, page * posts_per_page, posts_per_page); load_posts(req, posts, nposts, nposts == posts_per_page); str_putref(tag); req->body = render_page(req, tmpl); return 0; }
// Loads blog posts from database and displays them as bootstrap Panels // Also prints back, and forward buttons void print_blog_posts(int start, int end, char *search) { // How many posts are between start and end? int number_of_posts_requested = max(end-start+1, 0); struct Posts posts; if (search != NULL) { // Loads all posts that contain search posts = search_posts(search); } else { // Load all posts between start and end posts = load_posts(number_of_posts_requested, start); } struct Post *my_posts = posts.posts; // This could be less than number_of_posts_requested int number_of_posts_returned = posts.number_of_posts; // Print each posts for (int i = 0; i < number_of_posts_returned; i++) { // Use a panel for each post print_blog_post(my_posts[i].title, my_posts[i].text, my_posts[i].time, NULL); } // Free the whole list of posts free_posts(&posts); // Older and New buttons printf("<nav><ul class=\"pager\">"); // If we got the same number of posts we asked for, show the older button if (number_of_posts_requested == number_of_posts_returned) { printf("<li><a href=\"/cgi-bin/cblog.cgi?start=%d&end=%d\">Older</a></li>", end+1, end+5); } // If start > 0, show the newer button (means there are newer posts available) if (start > 0) { printf("<li><a href=\"/cgi-bin/cblog.cgi?start=%d&end=%d\">Newer</a></li>", start-5, start-1); } printf("</ul></nav>"); // Close older/newer buttons }