V. Rubinetti
V. Rubinetti

Reputation: 1696

convenient "chained" || (OR) expressions in liquid

In javascript, I can write something like this

console.log( person.name || person.nickname || '-' )

and it will first look for the name field, and if it can't find it, it will fallback to the nickname field, and if it can't find that, it will finally fallback to a - placeholder.

Is there any nice/convenient syntax to do this in liquid? That is, is there any way to do it in a single line, without a bunch of if/elseifs.

Upvotes: 1

Views: 103

Answers (1)

Mr. Hugo
Mr. Hugo

Reputation: 12582

No, sadly not. Although you can use 'or' in liquid logic, writing 'or' between double curly brackets is not allowed. You will get the following error:

Expected end_of_string but found id in "{{ person.name or person.nickname or '-' }}" in /path/to/page

I think that the shortest way to write this, is:

{{ person.name }}{% unless person.name %}{{ person.nickname }}{% unless person.nickname %}-{% endunless %}{% endunless %}

Upvotes: 1

Related Questions