We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

New Plugin: Modulo Operator

Development and Programming

David Dexter's avatar
David Dexter
88 posts
16 years ago
David Dexter's avatar David Dexter

I wrote a quick little plugin to calculate the modulo value and figured I’d share it here.

In computing, the modulo operation finds the remainder of division of one number by another.

Given two numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder, on division of a by n. For instance, the expression “7 mod 3” would evaluate to 1, while “9 mod 3” would evaluate to 0. Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands.

Parameters

1) Dividend (required) 2) Divisor (required)

{exp:modulo dividend="dividend" divisor="divisor"}

For example:

{exp:modulo dividend="3" divisor="3"} => 0
{exp:modulo dividend="4" divisor="3"} => 1
{exp:modulo dividend="5" divisor="3"} => 2
{exp:modulo dividend="6" divisor="3"} => 0

Hope it saves you some time…..

Best, David

       
Joobs's avatar
Joobs
362 posts
16 years ago
Joobs's avatar Joobs

I often use this (with php) to add classes to remove padding on the last image in a gallery in the weblog entries loop.

Can you use it like this (I haven’t tried)?

{if {exp:modulo dividend="{count}" divisor="3"} = 0}class="last"{/if}
       
David Dexter's avatar
David Dexter
88 posts
16 years ago
David Dexter's avatar David Dexter

Personally I use the switch tag to handle that issue. If I have a row with 3 items and I want the third to have a certain class, ‘endrow’ for example, I would do something like this:

<div class="normalclass {switch='||endrow'}">
path/to/image/
</div>

The result would be:

<div class="normalclass">
path/to/image/
</div>

<div class="normalclass">
path/to/image/
</div>

<div class="normalclass endrow">
path/to/image/
</div>

Hope that helps!

Best, David

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

First off - David thanks for a great plugin. I’ve done this in the past with PHP but it really annoys me doing things like that. I’ve always meant to make a plugin to do this and just never got around to it so this is great, thanks.

I often use this (with php) to add classes to remove padding on the last image in a gallery in the weblog entries loop. Can you use it like this (I haven’t tried)?
{if {exp:modulo dividend="{count}" divisor="3"} = 0}class="last"{/if}

Should work if you do it like this :

{if "{exp:modulo dividend="{count}" divisor="3"}" == "0"}class="last"{/if}

Best wishes,

Mark

       
TonyNibbles's avatar
TonyNibbles
98 posts
16 years ago
TonyNibbles's avatar TonyNibbles

omg this is so useful!

Thank you! 😊

       
MarkerMaker's avatar
MarkerMaker
220 posts
16 years ago
MarkerMaker's avatar MarkerMaker

OK I have found that neither of these solutions work…

Here is my code for both solutions. One using the new Modulo Operator.

Just to say the switch tag is not appropriate for my website layout.

If anyone could she some light it would be appreciated.

{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        <!-- Link -->
        <div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <h1>{title}</h1>
            <h3>{link_url}</h3>
            {link_description}
        </div>
        <!-- // Link -->
            {if "{exp:modulo dividend="{count}" divisor="3"}" == "0"}
                <div class="clear"></div>
            {/if}
        {/exp:weblog:entries}
{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        <!-- Link -->
        <div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <h1>{title}</h1>
            <h3>{link_url}</h3>
            {link_description}
        </div>
        <!-- // Link -->
            {if count == 3 || count == 6 || count == 9 || count == 12 || count == 15 || count == 18 || count == 21}
                <div class="clear"></div>
            {/if}
        {/exp:weblog:entries}
       
David Dexter's avatar
David Dexter
88 posts
16 years ago
David Dexter's avatar David Dexter

Hey Marker,

If you output the tag outside of the if statement is it returning the desired result:

i.e.

{exp:modulo dividend="3" divisor="3"}

Thanks, David

       
MarkerMaker's avatar
MarkerMaker
220 posts
16 years ago
MarkerMaker's avatar MarkerMaker

Thanks for your reply, you will have to excuse me I am not a developer per say.

How do you mean can you show me?

M.

       
David Dexter's avatar
David Dexter
88 posts
16 years ago
David Dexter's avatar David Dexter

If you put exactly this:

{exp:modulo dividend="3" divisor="3"}

in you template. What does it show… (It should show a zero)

       
MarkerMaker's avatar
MarkerMaker
220 posts
16 years ago
MarkerMaker's avatar MarkerMaker

Yes it does, it’s shows a zero. What does this mean?

Thanks M.

       
David Dexter's avatar
David Dexter
88 posts
16 years ago
David Dexter's avatar David Dexter

Try this:

{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        <!-- Link -->
        <div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <h1>{title}</h1>
            <h3>{link_url}</h3>
            {link_description}
        </div>
        <!-- // Link -->
        {switch="||<div class="clear"></div>"}
{/exp:weblog:entries}
       
MarkerMaker's avatar
MarkerMaker
220 posts
16 years ago
MarkerMaker's avatar MarkerMaker

well, it worked on part outputting ‘<div class=’ to every third entry.

Any ideas?

       
David Dexter's avatar
David Dexter
88 posts
16 years ago
David Dexter's avatar David Dexter

Yeah, change the quotes to single quotes. I’d also add comments inside the div for better browser compatibility.

{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        <!-- Link -->
        <div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <h1>{title}</h1>
            <h3>{link_url}</h3>
            {link_description}
        </div>
        <!-- // Link -->
        {switch="||<div class='clear'><!-- --></div>"}
{/exp:weblog:entries}
       
MarkerMaker's avatar
MarkerMaker
220 posts
16 years ago
MarkerMaker's avatar MarkerMaker

Yes that worked a treat many thanks.

I also tried a method using the if tag which was…

{if count == 3 || count == 6}
do this...
{/if}

This one was odd because it worked in some places in my site but not in others, and I could not understand why as the code was all identical.

Many thanks fro your help, much appreciated.

I would love to see the plug-in you created in action. Perhaps you should put up online somewhere some working examples so non developers like me can grasp what it can be used for?

M.

       
vladyn's avatar
vladyn
176 posts
16 years ago
vladyn's avatar vladyn
Yeah, change the quotes to single quotes. I’d also add comments inside the div for better browser compatibility.
{exp:weblog:entries weblog="links" sort="asc" disable="categories|member_data|pagination|trackbacks|comments"}
        <!-- Link -->
        <div class="{switch="grid_3 alpha link|grid_3 link|grid_3 omega link"}">
            <h1>{title}</h1>
            <h3>{link_url}</h3>
            {link_description}
        </div>
        <!-- // Link -->
        {switch="||<div class='clear'><!-- --></div>"}
{/exp:weblog:entries}

Hmm - it’s not working for - it does not output any div clear?

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.