How to Increment/Count in Liquid

Doug16
Shopify Partner
2 0 3

For some reason that is so far beyond me I can't even fathom how the decision was made, you can't count, increment or do any real math in Liquid. Sure, whatever. 

However, you are able to manipulate strings really well, so here's what I did to count search results that were grouped, thus preventing me from using {{search.results.count}} straight up.

I use a prefix in the handle to segment the search results. The trick is to append chars to an initially zero-length string, then get the 'size' of the string when you're done.

{% assign protocols_count = '' %}
{% for i in search.results %}
  {% if i.url contains '/pages/p-' %}
    {% assign protocols_count = protocols_count | append: 'x' %}
  {% endif %}
{% endfor %}

And then to display the value in the tab, I use:

{{ protocols_count.size }}

Ta-dah! A count of how many search results that match my filter.

Here's it in use in the real world: http://homogenizers.net/search?tab=appcenter&q=cell

Is there another way to do this? Just curious to see your thoughts on this. 

cheers, doug.
Gray1
Shopify Expert
156 0 27

Counting is actually really easy! It just doesn't make the most sense in liquid:

{% assign count = 0 %}

{% for something in somethings %}
  {% assign count = count | plus: 1 %}
{% endfor %}

{{ count }}

 

Developer for Pixel Union
Jason
Shopify Expert
10037 119 1873

Or you could just use increment.

{% assign foo = 1 %}
{% increment foo %}
{{ foo }} // outputs 2

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Gray1
Shopify Expert
156 0 27

Oh that's fantastic! I didn't know about that one.

Developer for Pixel Union
Doug16
Shopify Partner
2 0 3

I guess I was looking at old docs. I just found a bunch of people complaining that there was no way to increment. thanks for this!

 

cheers, doug.
markf84
Shopify Partner
2 0 0

Sorry for digging up an old thread, but I had a question relating to this topic that I would really appreciate some help on.

 

I have a section block that I need to increment a number for each block, but I need to use the count multiple times in a block.  I need the count to remain the same number so that matching elements can have the same ID and FOR properties.  For instance, with this implementation, the input ID is faq0 and the matching label's for property is faq1, when I need them to both be 0:

 

 

{% assign count = 0%}
<div class="faq-block"> <input id="faq{% increment count%}" type="radio" name="faqaccordion" /> <label class="faq-block__question" for="faq{% increment count%}"><h4> {{ block.settings.title }} </h4></label> <div class="faq-block__answer"><p> {{ block.settings.richtext }} </p><div> </div>

 

EDIT:  Ive actually solved my own question, but instead of using iteration, I just called the block.id on both of those properties.  Still wouldn't mind knowing if there is an answer to the previous question however!

0 Likes
Ninthony
Shopify Partner
1634 224 613

couldn't you just use {{ count }} the second time instead of incrementing?

 

{% assign count = 0%}
<div class="faq-block">   
        <input id="faq{% increment count%}" type="radio" name="faqaccordion" />
        <label class="faq-block__question" for="faq{{ count }}"><h4> {{ block.settings.title }} </h4></label>
        <div class="faq-block__answer"><p> {{ block.settings.richtext }} </p><div>
</div> 
If my solution helped you, please like and accept as solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes
markf84
Shopify Partner
2 0 0

Not within a for loop:

{% assign count = 0 %}
    {% for block in section.blocks %}  
      <div class="faq-block faq-block--{{ block.id }}">   
        <input id="faq{% increment count %}" type="radio" name="faqaccordion" />
        <label class="faq-block__question" for="faq{{ count }}">{{ block.settings.title }}</label>
        <div class="faq-block__answer">{{ block.settings.richtext }}</div>
      </div> 
    {% endfor %}

The second call for just the {{ count }} returns what the variable is set to originally, not what it is after it's been incremented.  In the above example, the input id becomes faq1 and the label's for becomes faq0.

0 Likes
Ninthony
Shopify Partner
1634 224 613

Hmm, thats weird I would think that the increment of the variable count would change it's value, I haven't used it much at all in liquid. If I really needed them to be the same I would just make another count variable called count_2 or something and increment that if I couldn't figure out another way to do it like you did. 

If my solution helped you, please like and accept as solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes
Ninthony
Shopify Partner
1634 224 613

Also I just checked and the assignment isnt even necessary, you could just increment and it starts at 0. So you could do:

 

    {% for block in section.blocks %}  
      <div class="faq-block faq-block--{{ block.id }}">   
        <input id="faq{% increment count %}" type="radio" name="faqaccordion" />
        <label class="faq-block__question" for="faq{% increment count2 % }">{{ block.settings.title }}</label>
        <div class="faq-block__answer">{{ block.settings.richtext }}</div>
      </div> 
    {% endfor %}
If my solution helped you, please like and accept as solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes