Check for a string in an array – Liquid vs Nunjucks.

While incorporating more of IndieWeb into my website, I found myself implementing an identical logic in two templating languages: Liquid and Nunjucks.

To check whether a string exists in an array, this is how you would do it in each language.

In Liquid:

{% if data.publish-to contains 'mastodon' %}

In Nunjucks:

{% if 'mastodon' in data['publish-to'] %}

Accessing properties with special characters

In our example, another thing of note is how a property on the data object with a hyphen in its name β€” publish-to β€” is being retrieved.

Liquid comfortably understands what is called the dot notation in JavaScript. This is quite impressive.

Nunjucks needs what is called the bracket notation in JavaScript.