ruby-on-railsliquidlocomotivecms

Increment value in locomotivecms liquid markup


I wouldn't have come here if I hadn't tried many different approaches...

Obviously collections of objects do have a field size/length, but the components don't have an index. I want something like this:

{% for product in contents.products %}

<h3>Produkt {{ product.index + 1 }}</h3>
<p>{{ product.price | concat: ' €' }}</p>


{% endfor %}

I have tried the following as documented here: http://www.omniref.com/ruby/gems/locomotivecms-liquid/classes/Liquid::Increment

{% increment variable %} 

Doesn't work. I have to work in the backend editor which complains about bad syntax. Unknown tag increment. Could I be working with an old version? Unfortunately I can't check it.

I also tried assigning a value to 0 before the for loop:

{% assign x = 0 %}

And then manually increment it by 1:

{% assign x = x + 1 %}

There must be way! I mean this is basic programming. Has anybody found a way around this? Thanks!


Solution

  • You can do increment in Locomotive CMS this way:

    {% assign counter = 0 %}
    {% capture counter %}{{ counter | plus: 1 }}{% endcapture %}