Collecting my attempts to improve at tech, art, and life

Draft Mode in Jekyll Templates

Tags: jekyll tools

tldr

Use site.show_drafts in templates if the local and live versions of your Jekyll site need to be different.

Yesterday I published a post about Jekyll collections. Today I checked Google Analytics to see if anybody looked at my site. 99 visits! Hey, nice. But I also noticed several “localhost” entries: those times I was double-checking my page locally with jekyll serve -Dw counted as visits, because the browser saw the analytics code and dutifully notified Google’s servers. So - less than 99 visits to my site. Oh well.

At some point I may disconnect Analytics altogether and go back to analyzing server logs directly. That certainly solves the localhost entries. Today I only want to adjust Jekyll’s build process so that it skips analytics code when building drafts.

Turns out that the configuration documentation lists show_drafts: null down there in the default configuration. I only needed an unless block in my template.

{% unless site.show_drafts %}
  {% include analytics.html %}
{% endunless %}

I added a similar block for comments, since Disqus handles those - another external service I don’t need in draft mode.

You may need something more elaborate than this for your needs, such as ad code for a live site and a placeholder image when building drafts. No matter what the details, site.show_drafts provides the key that you need.


Got a comment? A question? More of a comment than a question?

Talk to me about this page on:

Added to vault 2024-01-15. Updated on 2024-02-01