|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Jekyll 201: Beyond Hello World" |
| 4 | +categories: |
| 5 | +- blog |
| 6 | +--- |
| 7 | + |
| 8 | +There is [plenty of material][j1] [online showing you][j2] how to |
| 9 | +[setup a Jekyll blog][j3], [write your first post][j4] and |
| 10 | +[add a custom domain][j5]. But rarely do these tutorials go past |
| 11 | +the ["Hello World" stage][j6]. |
| 12 | + |
| 13 | +[j1]: http://benhowdle.im/2013/06/02/jekyll-in-15-minutes/ |
| 14 | +[j2]: http://destroytoday.com/blog/hello-world-im-jekyll/ |
| 15 | +[j3]: http://danielmcgraw.com/2011/04/14/The-Ultimate-Guide-To-Getting-Started-With-Jekyll-Part-1/ |
| 16 | +[j4]: http://ianreah.com/2012/12/27/An-introduction-to-Jekyll.html |
| 17 | +[j5]: https://help.github.com/articles/setting-up-a-custom-domain-with-pages |
| 18 | +[j6]: https://github.com/burkemw3/Heroku-Jekyll-Hello-World |
| 19 | + |
| 20 | +So it's time for Jekyll 201! |
| 21 | + |
| 22 | +I've been using Jekyll for blogs, meetup groups, prototyping, and microsites for |
| 23 | +nearly 3 years so I wanted to share some more advanced techniques that aren't |
| 24 | +covered in the docs. |
| 25 | + |
| 26 | +## Partials |
| 27 | +The basics of partials (`includes` in Jekyll parlance) are covered in the |
| 28 | +docs: [http://jekyllrb.com/docs/templates/#includes](http://jekyllrb.com/docs/templates/#includes) |
| 29 | + |
| 30 | +If you are coming from a traditional web development background, you might want |
| 31 | +to know how to pass in data to a partial. Maybe you have some book |
| 32 | +recommendations in your sidebar, like so: |
| 33 | + |
| 34 | +{% highlight html %} |
| 35 | +<ul id="book-recommendations"> |
| 36 | + <li class="recommendation"> |
| 37 | + <img src="/img/pragmatic-programmer.png" /> |
| 38 | + <a href="http://pragprog.com">Pragmatic Programmer</a> |
| 39 | + <span>My favorite book about programming pragmatically</span> |
| 40 | + </li> |
| 41 | + |
| 42 | + <li class="recommendation"> |
| 43 | + <img src="/img/getting-real.png" /> |
| 44 | + <a href="http://gettingreal.37signals.com">Getting Real</a> |
| 45 | + <span>A book about Getting Real and stuff...</span> |
| 46 | + </li> |
| 47 | +</ul> |
| 48 | +{% endhighlight %} |
| 49 | + |
| 50 | +You can make a partial (e.g. `_includes/recommendation.html`) so you aren't |
| 51 | +copy-pasting HTML code: |
| 52 | + |
| 53 | +{% highlight html %}{% raw %} |
| 54 | +<li class="recommendation"> |
| 55 | + <img src="{{ img_url }}" /> |
| 56 | + <a href="{{ url }}">{{ name }}</a> |
| 57 | + <span>{{ summary }}</span> |
| 58 | +</li> |
| 59 | +{% endraw %}{% endhighlight %} |
| 60 | + |
| 61 | +And then use it like so: |
| 62 | + |
| 63 | +{% highlight ruby %}{% raw %} |
| 64 | +<ul id="book-recommendations"> |
| 65 | + {% assign img_url = "/img/pragmatic-programmer.png" %} |
| 66 | + {% assign url = "http://pragprog.com" %} |
| 67 | + {% assign name = "Pragmatic Programmer" %} |
| 68 | + {% assign summary = "My favorite book about programming pragmatically" %} |
| 69 | + {% include recommendation.html %} |
| 70 | + |
| 71 | + {% assign img_url = "/img/getting-real.png" %} |
| 72 | + {% assign url = "http://gettingreal.37signals.com" %} |
| 73 | + {% assign name = "Getting Real" %} |
| 74 | + {% assign summary = "A book about Getting Real and stuff..." %} |
| 75 | + {% include recommendation.html %} |
| 76 | +</ul> |
| 77 | +{% endraw %}{% endhighlight %} |
| 78 | + |
| 79 | +We can re-use the HTML template but this is still not so great. Liquid doesn't |
| 80 | +have the ability to pass in a hash of local objects (like in Rails), instead we |
| 81 | +have to rely on essentially global variables. |
| 82 | + |
| 83 | +## Abusing `_config.yml` |
| 84 | +A better way to handle the above case of book recommendations is to store the |
| 85 | +underlying data in `_config.yml`. This file is typically used to store configuration |
| 86 | +options for Jekyll, but any extra YAML data you put in `_config.yml` will be |
| 87 | +accessible in templates via the `site` variable. |
| 88 | + |
| 89 | +So the `_config.yml` would look like this: |
| 90 | + |
| 91 | + # General Jekyll Config |
| 92 | + auto: true |
| 93 | + pygments: true |
| 94 | + exclude: [".gitignore", "readme.md", "Gemfile", "Gemfile.lock"] |
| 95 | + |
| 96 | + # Book recommendations |
| 97 | + recommendations: |
| 98 | + - img_url: /img/pragmatic-programmer.png |
| 99 | + url: http://pragprog.com |
| 100 | + name: Pragmatic Programmer |
| 101 | + summary: | |
| 102 | + My favorite book about programming pragmatically |
| 103 | + |
| 104 | + - img_url: /img/getting-real.png |
| 105 | + url: http://gettingreal.37signals.com |
| 106 | + name: Getting Real |
| 107 | + summary: | |
| 108 | + A book about Getting Real and stuff... |
| 109 | + |
| 110 | +Caveats: changes to this file are not picked up when running Jekyll locally with |
| 111 | +the `--auto` flag, so you need to regenerate the site manually for changes to |
| 112 | +take effect (several hours of my life I will never get back). |
| 113 | + |
| 114 | +Make sure all of your data is valid YAML (here is a good [linter][yl]). |
| 115 | + |
| 116 | +Then in your template, you can loop over all the book recommendations. |
| 117 | + |
| 118 | +{% highlight ruby %}{% raw %} |
| 119 | +<ul id="book-recommendations"> |
| 120 | + {% for recommendation in site.recommendations %} |
| 121 | + {% include recommendation.html %} |
| 122 | + {% endfor %} |
| 123 | +</ul> |
| 124 | +{% endraw %}{% endhighlight %} |
| 125 | + |
| 126 | +And update the partial to use properties on `recommendation`... |
| 127 | + |
| 128 | +{% highlight html %}{% raw %} |
| 129 | +<li class="recommendation"> |
| 130 | + <img src="{{ recommendation.img_url }}" /> |
| 131 | + <a href="{{ recommendation.url }}">{{ recommendation.name }}</a> |
| 132 | + <span>{{ recommendation.summary }}</span> |
| 133 | +</li> |
| 134 | +{% endraw %}{% endhighlight %} |
| 135 | + |
| 136 | +Much better! And now, when you want to add a new book, just add it to the list |
| 137 | +in `_config.yml` and it will get rendered when you regenerate the site. |
| 138 | + |
| 139 | +I've used this technique several times with success, here are a few examples: |
| 140 | +* [Indy Startup Lab][isl]: listing the members and projects, [repo here][islc] |
| 141 | +* [Let's Work Happier][lwh]: microsite with app directory, [repo here][lwhc] |
| 142 | + |
| 143 | +## Adding extra fields to posts |
| 144 | +Pages have some default attributes that you can set in the [Front-matter][fm]; |
| 145 | +these include the basics like permalink, title, categories, and tags. |
| 146 | + |
| 147 | +But you are not limited to just these fields. You can add any arbitrary data to |
| 148 | +the Front-matter (it is just a block of YAML after all) and they will be exposed |
| 149 | +on the `page` variable. |
| 150 | + |
| 151 | +Let's say you write reviews on your blog sometimes and use Amazon Affiliates. Per |
| 152 | +the affiliate guidelines, you need a disclaimer on your post stating that you |
| 153 | +are participating in the program or the FCC will get mad. |
| 154 | + |
| 155 | +You could copy and paste it to the bottom of each Markdown post — but a better |
| 156 | +way would be to set a custom attribute in the Front Matter. |
| 157 | + |
| 158 | + --- |
| 159 | + layout: post |
| 160 | + title: "My Favorite Gadgets Reviewed" |
| 161 | + disclaimer: true |
| 162 | + --- |
| 163 | + |
| 164 | +By adding `disclaimer` to the Front-matter, we can use `page.disclaimer` in the |
| 165 | +`post` layout to display our affiliate text at the bottom of the post when |
| 166 | +necessary. |
| 167 | + |
| 168 | +{% highlight html %}{% raw %} |
| 169 | + {% if page.disclaimer %} |
| 170 | + <div class="disclaimer"> |
| 171 | + <p> |
| 172 | + Disclaimer: Matt Swanson is a participant in the Amazon Services LLC |
| 173 | + Associates Program, an affiliate advertising program designed to provide |
| 174 | + a means for sites to earn advertising fees by advertising and linking |
| 175 | + to amazon.com |
| 176 | + </p> |
| 177 | + </div> |
| 178 | + {% endif %} |
| 179 | +{% endraw %}{% endhighlight %} |
| 180 | + |
| 181 | +This technique is pretty powerful, here a few other ideas you can use it for: |
| 182 | +* [Author attribution][aa]: useful for multiple author blogs/sites |
| 183 | +* Enable/disable Disqus/LiveFyre comments on a per-post basis |
| 184 | +* Add summary, subtitles, per-post header images |
| 185 | +* Display a banner for your new startup at the bottom of posts |
| 186 | + |
| 187 | +## Ghetto Asset Pipeline |
| 188 | +A little trick I picked up from the [DevelopmentSeed blog][ds] — you can use |
| 189 | +`includes` to concatenate your CSS files. |
| 190 | + |
| 191 | +Move your CSS files into the `_includes` folder (I like to put them in a `css` |
| 192 | +sub-folder). Then make an `all.css` file with an empty Front Matter, here is the |
| 193 | +one from the blog you are currently reading. |
| 194 | + |
| 195 | +{% raw %} |
| 196 | + --- |
| 197 | + --- |
| 198 | + {% include css/font-awesome.css %} |
| 199 | + {% include css/base.css %} |
| 200 | + {% include css/skeleton.css %} |
| 201 | + {% include css/screen.css %} |
| 202 | + {% include css/layout.css %} |
| 203 | + {% include css/syntax.css %} |
| 204 | +{% endraw %} |
| 205 | + |
| 206 | +Voilà! Six asset requests down to one! |
| 207 | + |
| 208 | +## Custom Plugins |
| 209 | +The new Jekyll docs have a great section on [writing plugins][plugin] with several |
| 210 | +examples so I won't dive into it here. |
| 211 | + |
| 212 | +Be warned — you cannot use custom plugins with GitHub pages. I am of the opinion |
| 213 | +that GitHub pages is a huge benefit to using Jekyll, so be careful about relying |
| 214 | +on plugins. |
| 215 | + |
| 216 | +Often times you can reproduce the functionality with Javascript or a Modular |
| 217 | +Component (see below). Personally, whenever I reach for a custom plugin I step |
| 218 | +back and evaluate whether I really need the functionality at all — usually the |
| 219 | +answer is no. |
| 220 | + |
| 221 | +## Building Modular Components |
| 222 | +Static sites, by design, are very limited. In a world where you can build an |
| 223 | +entire business on a WordPress blog, Jekyll seems very simplistic. |
| 224 | + |
| 225 | +For the most part, I think this is good — I believe in putting the emphasis back |
| 226 | +on content. But there are times when you miss some of the conveniences of heavier |
| 227 | +content platforms. |
| 228 | + |
| 229 | +You can add some of this functionality back by using Modular Components. The |
| 230 | +basic concept is to write smaller services that your blog can interact with. By |
| 231 | +offloading the backend work to other apps you can get around some of the limits |
| 232 | +of a static site. |
| 233 | + |
| 234 | +The most discussed example is using a 3rd party comment system — so I will skip |
| 235 | +that. Two other use cases that I think are more interesting are an email form |
| 236 | +and a web-based post editor. |
| 237 | + |
| 238 | +For the email form, I created a simple [Ruby app][mailer] that accepts |
| 239 | +cross-domain POSTs and sends emails to the officer list for a local meetup. The |
| 240 | +Jekyll site just serves up the form with the action pointing to the mailer |
| 241 | +app. Not perfect, but certainly better than setting up a whole CMS. |
| 242 | + |
| 243 | +[Prose][prose] is an app that allows you to edit and create posts on GitHub |
| 244 | +pages with a web interface by connecting to the GitHub API. I personally still |
| 245 | +stick with the command line and SublimeText for writing my posts, but those of |
| 246 | +you that miss having a WYSIWYG web editor for your content should check out |
| 247 | +Prose. |
| 248 | + |
| 249 | +--- |
| 250 | + |
| 251 | +Are there more Jekyll topics you want to see covered? Hit me up on |
| 252 | +[Twitter](https://twitter.com/_swanson). |
| 253 | + |
| 254 | +[fm]: http://jekyllrb.com/docs/frontmatter/ |
| 255 | +[plugin]: http://jekyllrb.com/docs/plugins/ |
| 256 | +[isl]: http://indystartuplab.org |
| 257 | +[islc]: https://github.com/IndyStartupLab/indystartuplab.org |
| 258 | +[lwh]: http://letsworkhappier.com |
| 259 | +[lwhc]: https://github.com/sep/letsworkhappier.com |
| 260 | +[aa]: http://www.lostdecadegames.com/blog-author-attribution-using-jekyll/ |
| 261 | +[yl]: http://yamllint.com/ |
| 262 | +[ds]: http://developmentseed.org/blog/2011/09/09/jekyll-github-pages/ |
| 263 | +[mailer]: https://github.com/indy-software-artisans/jekyll-mailer |
| 264 | +[prose]: http://developmentseed.org/blog/2012/june/25/prose-a-content-editor-for-github/ |
| 265 | + |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | + |
| 270 | + |
0 commit comments