Jane Doe

Check for a string in an ar­ray - Liquid vs Nunjucks

This post is over a year old! Information may be stale. Please read with cau­tion.

This post orig­i­nally ap­peared on rus­ingh.com.

While in­cor­po­rat­ing more of IndieWeb into my web­site, I found my­self im­ple­ment­ing an iden­ti­cal logic in two tem­plat­ing lan­guages: Liquid and Nunjucks.

To check whether a string ex­ists in an ar­ray, this is how you would do it in each lan­guage.

In Liquid:

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

In Nunjucks:

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

Accessing prop­er­ties with spe­cial char­ac­ters

In our ex­am­ple, an­other thing of note is how a prop­erty on the data ob­ject with a hy­phen in its name — publish-to — is be­ing re­trieved.

Liquid com­fort­ably un­der­stands what is called the dot no­ta­tion in JavaScript. This is quite im­pres­sive.

Nunjucks needs what is called the bracket no­ta­tion in JavaScript.