I would like to wrap every 3 rows that the matrix fields output with some required html like this:

{exp:channel:entries channel="pages" disable="categories|member_data|pagination"}
 {if rows are: FIRST FOURTH SEVENTH etc}
  <div class="row">
 <div class="span4">                   
 <p>{text} <a href="{link}" class="more">Read more</a></p>
 {if rows are THIRD SIXTH NINTH etc}
  </div><!-- / row -->

3 Answers 3


Ah, you want groups of three wrapped and not every third one, wrapped, right? @mediagirl almost has it, but if your returned entries is not evenly divisible by three, you'll have a problem - the final switch will not take effect. You therefore have to close the wrapper conditionally, like this:

{exp:channel:entries channel="pages" disable="categories|member_data|pagination"}

      {switch="<div class='row'>||"}

      Your per matrix row data here

      {if row_count != total_rows}{switch="||</div>"}{/if}{!-- closes row at switch interval IF last row IS NOT the current row --}
      {if row_count == total_rows}</div>{/if}{!-- closes row IF last row IS current row --}  


I've done this many times, and it's a great, if not often thought of uses for the switch variable.

  • 1
    Incidentally, this works with any loop that supports the switch variable. Commented Sep 4, 2013 at 15:07

Unfortunately, I don't think this is supported natively (thought I would love to be shown otherwise!). The way I achieved this was to use the MX Calculator extension to calculate whether a row's count is divisible by 3 or not:


        <span style="display: none;">
          {exp:mx_calc expression="mod({row_count}, 3)"}

            {if '{calc_result}' == '0'}
                divisible by 3: {number} <br />
                not divisible by 3: {number} <br />

This works and you can then customise every third row's outcome, but has a small inconvenience. When you use the exp:mx_calc opening tag to set the {calc_result} variable, it prints out the result of the expression. This prints out:

1 not divisible by 3: 1
not divisible by 3: 2
divisible by 3: 3
not divisible by 3: 4
not divisible by 3: 5
divisible by 3: 6

Unfortunately, I can't use the mx_calc tag right in the conditional, due to parse order issues. The work around is to just wrap the mx_calc tag in a span and give it display: none;

However, I unfortunately skim read your question (which I know I shouldn't do) and I thought you were talking about Grid. In Grid, you can just have mx_calc right in the conditional, because it's not a third party add-on. Thus:

        {if '{exp:mx_calc expression="mod({test:count}, 3)"}' == '0'}
            divisible by 3: {test:number} <br />
            not divisible by 3: {test:number} <br />
  • I would highly recommend doing this above the other ways. Doing a modulo calculation allows for flexibilty and is actually pretty much a native php-method (php.net/manual/en/internals2.opcodes.mod.php), and it is what you want to do instead of mimicing behavior.
    – Steven
    Commented Sep 3, 2013 at 18:24

Using the switch="" parameter may do what you need. This is untested but I don't see why it wouldn't work.

{switch="||<div class='row'>"}

I adjusted the quotes in my code example based on Jean's comment below...

  • 1
    This will work - I've done it myself - one change though - for whatever reason, I find it's buggy if you use single quotes on the outside and double on the inside. Strangely the bugginess goes away if you reverse that (so double quotes on the outside, single quotes on the inside). That's been my experience anyway up to and including EE 2.5.5. Commented Sep 3, 2013 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.