Per-Entry Pagination in Blogofile?

22 views
Skip to first unread message

pscht

unread,
Apr 2, 2012, 3:38:33 PM4/2/12
to blogofil...@googlegroups.com
We all know how to do per-blog pagination where you can navigate to the next page of entries from the index, but how, if at all, can you add Previous and Next links within the permapages of the entries?

I vaguely recall seeing a `${previous_link}` and `${next_link}` mention somewhere, but I have not been able to find a guide on the matter, nor have I managed to get a permapage to work with all conceivable permutations of the variables.

If anyone knows if this is possible, please do tell—preferably with a code example. I assume it goes in the `post.mako` template.

Mike Pirnat

unread,
Apr 2, 2012, 3:48:58 PM4/2/12
to blogofil...@googlegroups.com

I run a slightly-tweaked 0.7.

At the bottom of my _templates/permapage.mako, I have:

% if prev_post or next_post:
<p class="prev_next">
% if prev_post:
<a href="${prev_post}">« Previous Post</a>
% endif
% if prev_post and next_post:
|
% endif
% if next_post:
<a href="${next_post}">Next Post »</a>
% endif
</p>
% endif


Then in my _controllers/blog/permapage.py, my write_permapages looks
like this - the important bit is conditionally adding the
next/previous permalinks to the env dictionary:

def write_permapages():
"Write blog posts to their permalink locations"
site_re = re.compile(bf.config.site.url, re.IGNORECASE)
num_posts = len(blog.posts)

for i, post in enumerate(blog.posts):
if post.permalink:
path = site_re.sub("", post.permalink)
blog.logger.info("Writing permapage for post: {0}".format(path))
else:
#Permalinks MUST be specified. No permalink, no page.
blog.logger.info("Post has no permalink: {0}".format(post.title))
continue

env = {
"post": post,
"posts": blog.posts,
"next_post": None,
"prev_post": None
}

#Find the next and previous posts chronologically
if i < num_posts -1:
env['prev_post'] = site_re.sub("", blog.posts[i + 1].permalink)
if i > 0:
env['next_post'] = site_re.sub("", blog.posts[i - 1].permalink)

bf.writer.materialize_template(
"permapage.mako", bf.util.path_join(path, "index.html"), env)


Hope that helps!

--
Mike Pirnat
mpi...@gmail.com
http://mike.pirnat.com/

Marcin Kasperski

unread,
Apr 2, 2012, 4:51:00 PM4/2/12
to blogofil...@googlegroups.com
>        #Find the next and previous posts chronologically
>        if i < num_posts -1:
>            env['prev_post'] = site_re.sub("", blog.posts[i + 1].permalink)
>        if i > 0:
>            env['next_post'] = site_re.sub("", blog.posts[i - 1].permalink)

I do almost the same, but here I give template whole post object (just
strip .permalink from above). This way I can use actual titles in my
permapage.mako:

%if prev_post or next_post:
<div class="entrypaging">
##Prev and next entries:
<br/>
%if prev_post:
<span class="entrypaging_left">
«
<a href="${prev_post.permapath()}">${prev_post.title}</a>
</span>
%endif
<span class="epicon">|</span>
%if next_post:
<span class="entrypaging_right">
<a href="${next_post.permapath()}">${next_post.title}</a>
»
</span>
%endif
</div>
%endif


Whether to use titles or Prev/Next is of course a matter of taste, but
such parametrization gives more templating freedom…

PS In general it is fairly easy to patch elements of blog controller
to provide more data to templates and to generate additional pages
(like extra indexes based on different criteria).

Reply all
Reply to author
Forward
0 new messages