Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll arithmetic in conditional statement

Tags:

jekyll

liquid

I am trying to do some basic arithmetic in Jekyll's liquid templating engine. I have assigned one variable numColumns and I am trying to use it in a conditional statement.

{% assign numColumns = 3 %}

Note I omitted the outer for loop in the below expression where loopindex comes from. Regardless, this works with the - operator and correctly evaluates to 2.

{% if loopindex == 3 - 1 %}

However, these alternatives I tried do not work:

{% if loopindex == numColumns - 1 %}
{% if loopindex == numColumns | minus: 1 %}
{% if loopindex == {{ numColumns }} - 1 %}
{% if loopindex == {{ numColumns | minus: 1 }} %}

How can I subtract one from numColumns in a conditional statement with the liquid templating engine?

like image 655
ThisClark Avatar asked Aug 09 '15 05:08

ThisClark


1 Answers

You cannot use filter in liquid if expression.

You have to assign your calculation to a variable, then use it in your if tag.

{% assign calc = numColumns | minus: 1 %}
{% if loopindex == calc %}
like image 197
David Jacquel Avatar answered Nov 03 '22 17:11

David Jacquel