XF 2.2 How to count inside foreach in a template?

grantus

Active member
I've looked around but can't seem to find what I'm looking for.

I have my foreach loop:

Code:
<xf:foreach loop="$view" value="$value">
</xf:foreach>

But I need to get the count. I noticed there's count="$count" in the approval_queue template but apparently it doesn't work?

What would be the best way to do a count++ inside my foreach?
 
Solution
You can do <xf:foreach loop="$array" value="$item" i="$i"> to get the current count in $i, or you can just do {{ count($view) }} if you want the total count.
You can do <xf:foreach loop="$array" value="$item" i="$i"> to get the current count in $i, or you can just do {{ count($view) }} if you want the total count.
I was trying that but it doesn't seem to want to work.

For example, I want to get odd numbers so I tried this but no luck:

Code:
<xf:if is="{$i} % 2">
 // odd numbers
</xf:if>
 
That is just the name that was chosen for the iterator count attribute of the <xf:foreach> tag. Admittedly it's not super descriptive.

Consider HTML, where you could have <div class="class"> if you wanted, but you could also have <div class="anything-else">.
 
  • Like
Reactions: LPH
That is just the name that was chosen for the iterator count attribute of the <xf:foreach> tag. Admittedly it's not super descriptive.

Consider HTML, where you could have <div class="class"> if you wanted, but you could also have <div class="anything-else">.
Ok, I figured that. Makes sense! Thanks for the help.
 
Back
Top Bottom