The Modulus Operator

6 comments

In ExpressionEngine 2.7.1, we introduced the modulus (%) operator for use in conditionals. It’s a fantastic alternative to {switch=} when you need a graceful way to handle potential “remainder entries”.

So what does modulus do?

When you ask a computer to divide, you end up with a decimal number:

2.3333333333333 

In this case, 2.3333333333333. However, back in grade school, when you divided one number by another you had the quotient and the remainder:

2 remainder 1 

The modulus operator simply gives us the remainder of a division:

This comes in handy when you’re looking for the nth item in a series—such as every 3rd Channel Entry—since the remainder for the 3rd Channel Entry divided by 3 will be 0:

{if count == 0} 

Excellent, but how can you take advantage of this? If you have a design that uses a layout 3 columns wide, modulus can be your hero! With modulus, you have a clean way to handle every third entry and any remainders as well.

Let’s look at one way to use modulus in a template:

<div class="container">
        
{exp:channel:entries
            channel
="blog"
            
limit="10"
        
}
            {if count 
== 1}
                
<div class="row">
            
{/if}
    
            
<div class="column">
                <
h4>{title}</h4>
                
{blog_body}
            
</div>
    
            
{if count == OR count == total_results}
                
</div>
            
{/if}
        {
/exp:channel:entries}
    
</div

To start, compare the number of entries you want in a row against 1. You’re asking ExpressionEngine “Open a new row when the number of entries (3) has a remainder of 1”. Basically, start a new row beginning with the first entry, and then again after every third entry.

Next, the <div class="column"> element will contain each entry to one column, opening and closing with each entry returned.

You’ll want to properly close each row, so compare {count} with modulus or against {total_results}. The modulus comparison tells ExpressionEngine, “when the remainder of 3 divided into {count} equals 0, close the row.” The {total_results} comparison makes sure to close the final row for the last returned entry.

Comments & Feedback

  1. Nice. I know that “how to do something for every nth” is a question that has been asked of EE a lot in the past. Looks like this new operator will help out a lot with that.

    Picture of Kristen Grote

    Kristen Grote

  2. Is it neccessary to specify a limit?

    Picture of worfoual

    worfoual

  3. @worfual - not at all. Only thing to keep in mind: without the limit parameter, channel entries will return all the entries from a channel, or the latest 100 (if the channel has more than 100 total entries).

    However, there is nothing about using modulus that requires a limit.

    Picture of Dan Decker

    Dan Decker

  4. The latest 100 entry from a channel! Wat happend if I have more than 100 entries and want to show all?

    Picture of worfoual

    worfoual

  5. @worfoual

    The latest 100 entry from a channel!

    Indeed. 100 is an automatic limit to ensure proper performance. If there were no limit at all, imagine if your channel had 10,000 or 20,000 entries and ExpressionEngine tries to return them all? That would be a rather poor experience.

    The default limit of 100 is simply a fail-safe. You can easily override by explicitly using a higher limit in the parameter (101, 200, 900, etc.).

    Picture of Dan Decker

    Dan Decker

  6. Thanks, I was just afraid it was not possible at all.

    Picture of worfoual

    worfoual

You must be logged in to comment on this blog post