Moving While42 to Jekyll
2 months ago we opened a while42 chapter in Taipei. We had a first meeting in Taipei Harckerspace, sponsored by Gandi.
When Sylvain, on the organizers mailing-list, launched a call to help renew the website while42.org, I proposed to switch it from php/mysql pages to a much simpler github pages setup, based on jekyll. Everybody was happy with the idea so I went for it.
Easy setup
Jekyll was created in 2009 by Tom Preston-Werner, the founder of github, as a side project. Since then it has been integrated as a way to publish pre-generated static pages on github, mostly to present projects. But there are many people just using it as a hosting platform for their blogs.
The idea is to have some code generating static web pages out of templates and data files. With years, Jekyll added support for SASS and coffeescript preprocessing. And recently, an arbitrary data system was added. It can replace a small database by yaml files.
Pages generation
Jekyll is written in ruby, but for publishing pages on github, you don’t really need to have ruby installed. The processing is triggered for each git push
. But it still can be convenient to have ruby installed for rendering pages locally before pushing. If there is a syntax error in a template, github will send you a mail with details and will refuse to generate a new version. So it’s quite robust and fail safe.
The side effect of having pages pre-generated is that they are of course much faster to serve, but also there is no question about the security, the portability and things are all along much simpler to manage from an operational point of view.
Note that for local generation I also created a simple Dockerfile, so that people that don’t want to install ruby can still pre-generate the pages locally before push.
Template system
The template system used in Jekyll is named Liquid, it was developed by spotify for their own system and published as open source.
Templating is great, some common parts of pages can be extracted and replaced by includes, like in the case of our While42 photos at the bottom:
This is in _includes/home/photos.md
:
And this is included in both index.md
and fr/index.md
:
Use of variables
In templates, we can call variables defined either by the system, either by the configuration file _config.yml
.
For example we have in the config file:
So we can use it as {{ site.twitter_username }}
in _includes/footer.html
:
Data storage
This is the most amazing part of recent Jekyll versions. You can store arbitrary Yaml files in _data
and call them anywhere in the templates. So for the while42 website, for the right column where all the chapters are listed, I created a _data/chapters/
with:
_data/chapters/001_san_francisco.yml
_data/chapters/002_new_york.yml
_data/chapters/003_paris.yml
_data/chapters/004_miami.yml
_data/chapters/005_singapore.yml
_data/chapters/006_bucharest.yml
etc..
I gave them numbers to enforce the ordering with the | sort
liquid filter below. In _data/chapters/001_san_francisco.yml
we have:
This made possible to include this in the main template _include/home.html
:
The possibilities of liquid are a bit frustrating for a coder as there is no easy way to do a variable variable, but still, it does the job pretty well. In the data file we only need to declare the lines for social links we have, and the icon for it is only displayed if the value is there.
The main point here is that all the content of _data
is usable in templates by just replacing the path slashes with dots. So I also made another _data/chapters_to_be.yml
with:
- La Réunion
- Tours
And it’s included in a much simpler way as:
An optimal workflow
The side effect of treating the while42 website as a public repository is that organizers from each chapters can now just clone the repo, add their modifications on their very own data file, and issue a pull request to the main repo. The websites maintainers (Julien and Sylvain) just have to check if the yaml file is properly formatted, if photos are added correctly, and they merge de pull request, then the website is updated.
It didn’t wait long before we get the first pull requests on https://github.com/while42-org/while42-org.github.io:
- https://github.com/while42-org/while42-org.github.io/pull/2
- https://github.com/while42-org/while42-org.github.io/pull/3
More about static generation
For many webiste, that are not full-blown web applications, generating staticaly makes a lot of sense. Jekyll is a great tool but there are a whole lot of various frameworks in all possible languages that you can use for your own needs. Check out http://www.staticgen.com for a list of them.