Popular Posts Using Jekyll and GitHub Pages (or, Why I'm Moving To Django)
A couple days ago I was looking for a way to incorporate a “Recent Posts” and “Popular Posts” sidebar on posts in this blog, and on the listing page.
Recent posts are easy, as it’s as simple as adding something like the snippet below, to generate a list of the 5 most recent posts.
<div class="list-group">
{% for post in site.posts limit:5 %}
<a href="{{ post.url }}" class="list-group-item">{{ post.title }}</a>
{% endfor %}
</div>
This generates a pretty list of post titles that link to that post. However, when I came to try to create the “Popular Posts” section, I came across a stumbling block. I personally knew which posts were popular from Google Analytics (and sometimes comments), but I had no idea how to get Jekyll to know which were most viewed (or most commented).
I came across a couple projects that claimed to be able to add the functionality. One example is this Octopress plugin which uses your page’s Google Pagerank to decide what your most popular posts. Obviously this is better than nothing, but doesn’t necessarily correlate with what your blog viewers are most interested in. I came across another that used a Google Analytics API to pull down pageviews, but I’d rather not tie functionality to my website analytics.
Instead, I’m deciding to migrate my blog to another platform. Jekyll has served me well up until now, but it feels just a little restrictive. There’s the lack of a Content Management System (CMS) - currently I just commit directly from GitHub when I want to post a new post. There’s the lack of flexibility - any time I want to add new functionality (such as newsletter sign-ups for example) I have to use some external service and tie it back in.
The platform I have decided to migrate to is Django, a web framework written in Python. I am already familiar with Django having used it in several projects before, so it seems a logical choice. Specifically, I’m investigating using a CMS called Mezzanine. Mezzanine is a collection of Django apps which come together to provide an easy-to-use, and out-of-the-box working CMS. It also promises to be highly customisable and extensible.
I’ll need to move away from GitHub Pages, since they only support static websites (and Jekyll based ones). But that’s okay. There’s plenty of options out there. The hosting service I’m looking to use once I’m ready to deploy is Digital Ocean. They provide easily scalable, cheap, and performant hosting - I’ve also used them on other projects such as my university team project.
Hopefully by my next post (but possibly the one after) you’ll be reading these posts on a wonderful Mezzanine-based website.
Check out my other blog posts! If you found this post interesting, feel free to let me know either on Twitter (@Isaac_M_Jordan), or in the comments section below.
Enjoyed my post? Sign up to the newsletter to receive a small email when I post. No spam, I promise.
-
Isaac Jordan on Feb. 9, 2016, 10:29 p.m.
GitHub pages is not just for Jekyll. My blog, http://pydanny.com, is powered by Pelican (Python powered static site generator), and has been since 2012.
Isaac Jordan on Feb. 9, 2016, 10:27 p.m.
You could realistically have a script on your server that analyses your Nginx logs, outputs the top 5 posts in a .yml file, and rebuilds the site. Alternatively, you could output a JSON file and fetch it on your site with some JS.
Isaac Jordan on Feb. 9, 2016, 10:27 p.m.
It's true that there are options like that. I've seen similar recommendations on Reddit, but I feel like Jekyll is limiting what I can do with my site too much. Obviously the downside is that I lose my free hosting though...
Isaac Jordan on Feb. 9, 2016, 10:28 p.m.
It seems to me that the free hosting is the limiting factor! You can't read your Nginx logs on GitHub pages in the first place, so that hack wouldn't work. Yeah, static websites often force you into some rather "creative" solutions (sometimes, that may mean ugly). I'm wondering to what extent you could run the two simultaneously, to get the best of both worlds... Maybe that just makes it more complicated than a pure Django-based approach.
Isaac Jordan on Feb. 9, 2016, 10:28 p.m.
I think for a feature or two, like if I only wanted to add the popular posts and nothing else, then sticking with the static site might be easier - but I have some other things I want to add to the site as well. At that point it seems worth a complete re-write to have a more flexible base to build off of.
Even building the exact same functionality that this site has in static form is no simple task though. Defining all the Django models I need, using various apps, and tying everything together is taking some time.