Incrementing Variables In Liquid Without Outputting Them

- 1 answer

I am doing a for loop in shopify, I need to increment a variable.

However, when I do

{% increment variable %}

besides incrementing it, it shows the output on the screen!

I can't believe it. Is there a way to avoid this?

Thank you



This is by design, at it allows you to increment and display a variable at the same time. See the documentation.

assign only allows you to assign new variables (and not modify existing ones), so aside from creating a new tag, the easiest way is to use use capture to capture the output:

{% capture _ %}{% increment variable %}{% endcapture %}

That being said, perhaps it's time to re-consider why exactly you're doing this? Note that you already have forloop.index and forloop.index0 available for the loop index (once again, see the documentation).