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

A brief post detailing how liquid and nunjucks templating languages differ in checking for a string in an array, as well as retriving properties with special characters on objects.

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.


Comment via email.