How To Escape Liquid Template Tags In Jekyll

October 6, 2014

My blog has been on Github Pages for about a week now. So far, so good. I’ll say this again, Jekyll + Github Pages, a dev’s dream! I especially like the syntax highlighting provided by the Pygments plugin. So sweet, so simple.

One of the tricks I’ve learnt since is how to highlight Liquid Template code, within the Liquid template you’re working on!

This is accomplished using a combination of the HTML syntax highlighting made available by Pygments, and the {% raw %} tag provided by Liquid (Talk about inception!). For example, if you want to display the code snippet below:

{% if post.excerpt %}
{{ post.excerpt }}
{% else %}
{{ post.content | truncatewords:30 }}
{% endif %}

Then this is the code you should have in your markdown file:

Escaped Template Tag

Easy to select, huh? :stuck_out_tongue_winking_eye:

Grab the gist here!

Happy hacking!