CMS Development

JWiberg
メンバー

How do i create a counter inside a loop in Hubspot?

解決

Hey,

 

Im curious how i can create a counter inside a for loop in hubspot? I have a for loop that spit outs different links right now, but i need to be able to keep the count of how many iteration it does. How do i do it in HubL?

For example in javascript you can keep track of it like this:

for (let i = 0; i < 5; i++) {
 console.log(`Iteration number ${i}`);
}

 

In the same way i want to create a function inside my for loop on my hubspot page where i can set i to a number and for each iteration it increases by 1. I hope this makes sense:)

0 いいね!
1件の承認済みベストアンサー
NBaring
解決策
参加者

How do i create a counter inside a loop in Hubspot?

解決
 

 

    {% set items = ['one', 'two', 'three'] %}
       
    {% for item in items %}
       <h1>{{item}}</h1>
        <p>Iteration number {{loop.index}}</p>
    {% endfor %}

 

 
 

元の投稿で解決策を見る

3件の返信
Ntbrown
投稿者

How do i create a counter inside a loop in Hubspot?

解決
loop.index
i = i is defined ? i + 1 : 1
loop.index0
i = i is defined ? i + 1 : 0

Backwards....

loop.revindex
i = i Is defined ? i - 1 : loop.length
loop.revindex0
i = i Is defined ? i - 1 : loop.length - 1
0 いいね!
JWiberg
メンバー

How do i create a counter inside a loop in Hubspot?

解決

How would i put it in my code if my for loop looks like this?

{% for item in items %}
Here goes the content for the loop...
{% endfor %}

 

 

0 いいね!
NBaring
解決策
参加者

How do i create a counter inside a loop in Hubspot?

解決
 

 

    {% set items = ['one', 'two', 'three'] %}
       
    {% for item in items %}
       <h1>{{item}}</h1>
        <p>Iteration number {{loop.index}}</p>
    {% endfor %}