Extending Include Components in Jekyll

19 July 2019

This post will highlight some tips and tricks of the include tag to build more advanced components in Jekyll. The include docs explains it thoroughly, and this post will summarize it with some of my extra findings.

Include Tag

Jekyll provides a very convenient Liquid tag to import other codes, known as include. A simple include tag would look like this:

{{ include somecode.html }}

But there's more to it, you can use global Liquid variables in the included code too. Like so:

index.html
{% assign person = "Darren" %}
{% include somecode.html %}
somecode.html
<p>Hello, {{ person }}!</p>
Output
<p>Hello, Darren!</p>

Or if you prefer scoping your variables locally:

index.html
<!-- Either like this -->
{% include somecode.html person="Darren" %}

<!-- Or like this -->
{%- assign name = "Darren" -%}
{% include somecode.html person=name %}
somecode.html
<p>Hello, {{ include.person }}!</p>
Output
<!-- Either like this -->
<p>Hello, Darren!</p>

<!-- Or like this -->
<p>Hello, Darren!</p>

Notice we're accessing the variables passed by prefixing 'include.', and now we have a simple component functionality.

My Findings

During the development of my previous website (Now I'm using Gatsby), I needed to embed custom HTML code in my components. Of course, I could just write it and pass it directly as an argument, but then I can't use double quotes (") in my code.

That's where the capture tag comes in, we can wrap our code in it and the quotes will be automatically escaped! Syntax highlighting would work normally too in your favorite text editor.

index.html
<button onclick="hello('Darren')">Say hello</button>
somecode.html
<p>{{ include.content }}</p>
Output
<p><button onclick="hello('Darren')">Say hello</button></p>

And that's about the basics of Jekyll components.

Downsides

Do note that it's not recommended to overuse the include tag as mentioned in the docs. Too many includes will slow down build time.

I hope you enjoy reading!