While building the new DIGImend website I needed a way to truncate strings in Liquid, which is the template language Jekyll uses. Specifically, I needed to compare parts of page URLs when iterating over sub-pages of a page to generate menus and page lists.

To my surprise I couldn’t find any. The version used by Jekyll didn’t seem to support slice, and the only alternative, truncate, seemed to always replace the end of the truncated string with three dots (“…”, an ellipsis).

However, diving into Liquid source in search of an alternative, I found that truncate accepts optional second argument: a string to replace that ellipsis with. So I could write this:

{% assign page_base = page_dir | truncate: parent_dir_len, "" %}

When writing this post I found that the Liquid’s GitHub project wiki mentions this argument, but being otherwise quite low on details it wasn’t the first place to look to me.