I am completely stumped by how to count plus one towards a variable assigned via {% assign var = 0 %}
. It should be the most simple task. Here's what I've tried so far:
{% assign amount = 0 %}
{% for variant in product.variants %}
{% assign amount = amount + 1 %}
{% endfor %}
Amount: {{ amount }}
The result is always 0
. Maybe I'm overlooking something obvious. Maybe there is a better way altogether. All I want to archive is getting the number of iterations that are run.
The Counting Variable keeps track of the number of times the Repeat Dialog (or Loop) has been repeated. The number associated with a particular loop constitutes the value of the Counting Variable. That value is then appended to each variable name used in a Question which is part of that loop.
Unfortunately, you cannot assign JS variables to Liquid variables. The reason is Liquid is processed at serverside. What you can do is, create an element with JS and push that JS variable content to that element if you need to show the JS content on a page.
As {{ increment amount }}
will output your variable value and does not affect a variable defined by {% assign %}
, I suggest you to use {% capture %}
:
{% assign amount = 0 %}
{% for variant in product.variants %}
{% capture amount %}{{ amount | plus:1 }}{% endcapture %}
{% endfor %}
Amount: {{ amount }}
I agree this is verbose, but it's AFAIK the only working solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With