CMS Development

SD_dev
Member

How do I get the count of a repeating object in a custom module?

SOLVE

Hello,

 

Feels like such a simple thing to calculate, but I'm struggling to find a solution.

 

I have created a custom module, it holds different logos. I'm using the "for item in" loop  to display the logos in a grid.

 

When the number of logos present is > 4 I want to output a different layout and structure, with slider controls etc. So I would like to calculate the count of items and use this in an IF/ELSE structure for this logic.

 

I tried using .count on the repeater object "module.logo" to get the number of logos to make the logic work, like so:

 

module.logo.count()

 

I also tried:

 

module.logo.count('item')

 

But this returns 0 no matter how many logos have been added

 

Is there a simple way to get the count of repeater items in the module?

 

Or do I have to loop through them and add 1 to a variable to get a custom variable with the count in it?

 

I know I could add .count logic within the for loop but my code would be a lot cleaner if I could calculate this outside of the loop first.

0 Upvotes
1 Accepted solution
JBeatty
Solution
Guide | Diamond Partner
Guide | Diamond Partner

How do I get the count of a repeating object in a custom module?

SOLVE

Hi @SD_dev,

 

If I am understanding how you have set up your HUBL project, you should be able to use the filter length for this.

module.logo|length

This should output the length of "module.logo".

✔️ Was I able to help answer your question? Help the community by marking it as a solution.

Joshua Beatty
Software Developer with Pearagon

Still have questions? Let's Talk

View solution in original post

3 Replies 3
JBeatty
Solution
Guide | Diamond Partner
Guide | Diamond Partner

How do I get the count of a repeating object in a custom module?

SOLVE

Hi @SD_dev,

 

If I am understanding how you have set up your HUBL project, you should be able to use the filter length for this.

module.logo|length

This should output the length of "module.logo".

✔️ Was I able to help answer your question? Help the community by marking it as a solution.

Joshua Beatty
Software Developer with Pearagon

Still have questions? Let's Talk

Javierismo
Contributor | Elite Partner
Contributor | Elite Partner

How do I get the count of a repeating object in a custom module?

SOLVE

that simple haha, never cross my mind, thanks!

0 Upvotes
SD_dev
Member

How do I get the count of a repeating object in a custom module?

SOLVE

Aha! So simple. Thank you, much appreciated!

0 Upvotes