Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Counting rows in template language

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 896 Views
  • DNA
    Community Member
    24 Posts

    Counting rows in template language Link to this post


    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;
    if($j == 5) {
    echo '<div class="clear">&nbsp;</div>';

    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?

  • Sean
    Forum Moderator
    921 Posts

    Re: Counting rows in template language Link to this post

    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.


Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.