Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Counting rows in template language


Reply


2 Posts   908 Views

Avatar
DNA

Community Member, 24 Posts

14 March 2010 at 3:44pm

Edited: 14/03/2010 3:46pm

Hi,

I need some way to create x many objects add a clearing class and add another create x many objects add another clearing class and continue. Example = thumbnails of photos in a gallery. Yes i could use a table but I would run into the same problem. The kind of functionality I'm looking for is similar to Even and Odd, but not with a base of 2.

In php i would do something like:
$j =0;
for ($i =0; $i < $total; $i++) {
echo $something;
$j++;
if($j == 5) {
echo '<div class="clear">&nbsp;</div>';
$j=0;
}
}

What I've done is added the below code to ViewableData:
/*
    * Return if we have a complete row or not
    * @param int $denominator, which defaults to 1.
    * @return boolean
    */
   function IsDivisibleBy($denominator = 1) {
      if (is_int(($this->iteratorPos+1) / $denominator)) {
         return true;
      } else {
         return false;
      }
   }

I can then use the below in my template class:
<% if IsDivisibleBy(3) %>

Is there a better way?

Avatar
Sean

Forum Moderator, 922 Posts

14 March 2010 at 7:54pm

2.4 added a modulus factor into ViewableData. The change was applied here: http://open.silverstripe.org/changeset/95584

You could probably use something like that. It uses modulus in PHP (%) to do what you're looking for.

Sean