Introduction
I use Jekyll to generate more than a few web sites. I like the separation between the HTML presentation and the Markdown content, and I like that its easy to migrate Jekyll web sites to and from GitHub Pages.
Recently, I decided to update one of those web sites, partly to take it out of the dark ages, and partly to learn more about Twitter Bootstrap. Twitter Bootstrap is a terrific package, consisting of Javascript, CSS and HTML that is relatively easy to use, flexible, and customizable.
Twitter Bootstrap
This article isn’t about Twitter Bootstrap, though. If you want to know more about this awesome package, here are some useful links:
- The original Twitter Dev Blog announcement
- The Twitter Dev Blog entry on Twitter Bootstrap 2.0
- The Twitter Bootstrap web site.
- Connor Turnbull’s Stepping Out with Bootstrap from Twitter
- Ryan Bates’ Railscast #328, Twitter Bootstrap Basics, which is Rails-specific, but still provides a good introduction to Twitter Bootstrap.
- David Cochran’s Twitter Bootstrap 101: The Grid
Once you’re convinced of its awesomeness, you’ll be ready to start using it.
Integrating with Jekyll
There are, obviously, many ways to integrate Twitter Bootstrap with Jekyll. In this article, I merely describe how I did it.
First, the easy way
While you’re getting your feet wet, use the Twitter Bootstrap Customize and download page to build and customize a Bootstrap package. From there, you can generate a zip file containing the Bootstrap images, compiled CSS files, and bundled Javascript (including minified and unminified versions).
To make it easy to update, unpack the resulting zip file in a bootstrap
directory, underneath your Jekyll source directory. Then, drop it into your
HTML layout(s). For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
Then, regenerate your site via Jekyll. When you generate your zip file (above), be sure to include the Collapse plugin. With that plugin in place, and the following line in your template, your navbar will automatically collapse into a button when the screen size is too small–which is much friendlier to mobile devices.
1 2 3 4 5 |
|
Going local
After awhile, you’re going to get tired of going back out to the Twitter Bootstrap site to generate a new zip file, every time you want to tweak your site’s look and feel. At that point, it’s time to start building your own Bootstrap bundle.
I chose a simple approach:
- Clone the Twitter Bootstrap GitHub repo into a local directory outside my Jekyll source.
- Write some simple Rake logic to copy the pieces I want, compiling the CSS, and minifying the Javascript in the process.
Clone the repo
For the sake of this article, let’s assume I cloned the Twitter Bootstrap GitHub repo as follows:
1 2 |
|
I’m going to build my Rake logic with the assumption that the Bootstrap
code is in ~/src/open-source/bootstrap
. Keeping these files in a separate,
pristine directory means:
- I can easily share them among different Jekyll projects, and
- I can update the repository easily and cleanly.
What Rake has to do
Rake is going to perform several tasks:
- If any of the Javascript files in the Bootstrap directory has changed,
copy it to the local Jekyll
bootstrap
directory, minifying it with the Ruby Uglifier gem. - Generate a partial (in the Jekyll
_includes
directory) that contains<script>
tags for all the Bootstrap Javascript files. Then, my layouts can simply include that file. - Bootstrap’s CSS files are actually written using the LESS language. If any of the Bootstrap LESS files has changed, copy it to the local Jekyll directory.
- Compile all the LESS files (including a local
custom.less
that contains my CSS overrides) into onebootstrap.min.css
file.
Prerequisites
If you’re using Jekyll, you already have a version of Ruby installed. But you’re going to need some additional software to accomplish the tasks above.
First, install node.js and the Node Package Manager (npm). Once they’re
in place, use npm to install the less
package. I installed it in my home
directory:
1 2 3 4 5 |
|
Next, install the Uglifier gem and Rake (if you haven’t already installed Rake):
1 2 3 4 5 6 7 8 9 |
|
The Rakefile
Now you’re ready for the Rakefile.
Definitions and Utilities
First, let’s put some simple definitions at the top of the Rakefile:
1 2 3 4 5 |
|
Next, let’s write a function to determine if two files are different. Rather than just test the file modification times, let’s also check the content, using an MD5 digest.
1 2 3 4 5 6 7 8 9 10 11 |
|
That function goes in the Rakefile. (I put it at the bottom, out of the way of the tasks, but you can put it wherever you want.)
Let’s also create a bootstrap
task that invokes two subtasks, bootstrap_css
and bootstrap_js
. That way, testing the Bootstrap Rake logic will be as
simple as typing rake bootstrap
.
1
|
|
Copying the Javascript files
The bootstrap_js
task, which copies the Bootstrap Javascript, files has to do
three things:
- Detect which ones have changed.
- Copy and minify them to our local Jekyll Bootstrap directory.
- Generate the previously mentioned partial, in
_includes/bootstrap.js.html
This task is straightforward enough:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Note that I’m using ERB to generate the partial. You can do this inline, of course, without ERB; I just find the ERB to be a little more readable.
Copying and compiling the LESS file
The bootstrap_css
task, which copies the Bootstrap LESS files has three jobs:
- Detect which ones have changed.
- Copy them to our local Jekyll Bootstrap directory.
- Compile them, along with our local
custom.less
file, intobootstrap/css/bootstrap.min.css
.
First, let’s take a look at the custom.less
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
custom.less
pulls in bootstrap.less
. bootstrap.less
pulls in nearly
everything else we need. Thus, compiling custom.less
is sufficient to get
everything else. I’ve also included some sample customizations, based on the
information at http://twitter.github.com/bootstrap/less.html.
If you don’t want all the features normally bundled by bootstrap.less
, you
can simply pull out the ones you want, put them into custom.less
, and remove
the import of bootstrap.less
.
Let’s finish this section with the Rake task, bootstrap_css
:
1 2 3 4 5 6 7 8 9 10 |
|
Invoking Jekyll
We might as well have the Rakefile invoke Jekyll, while we’re at it. Since
Jekyll is written in Ruby, we could import Jekyll and invoke it that way,
but I’m doing it the lazy way, by invoking the jekyll
command.
1 2 3 4 5 |
|
Changing the layout
The final change is to make our layouts include the bootstrap.js.html
that’s
generated by the bootstrap_js
task. Simply change:
1
|
|
to:
1
|
|
Running it
Now, we can build our Bootstrap-enabled site with one command:
1 2 3 4 5 6 7 8 |
|
Example
I used this approach on my corporate web site, www.ardentex.com. My use of Twitter Bootstrap barely scratches the surface of what’s available.
Other approaches
- Jason Gritman has a Jekyll-Bootstrap-Template that can help you get up and running.
- If you’re blogging with Jekyll, check out Jekyll Bootstrap.