5

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"}
 {my_matrix_field}
 {if rows are: FIRST FOURTH SEVENTH etc}
  <div class="row">
 {/if}
 <div class="span4">                   
 <p>{text} <a href="{link}" class="more">Read more</a></p>
 </div>
 {if rows are THIRD SIXTH NINTH etc}
  </div><!-- / row -->
 {/if}
 {/my_matrix_field}
{/exp:channel:entries}
1

3 Answers 3

8

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"}
  {my_matrix_field}

      {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 --}  

  {/my_matrix_field}
{/exp:channel:entries}

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

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

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:

    {matrix_test}

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

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

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:

    {test}
        {if '{exp:mx_calc expression="mod({test:count}, 3)"}' == '0'}
            divisible by 3: {test:number} <br />
        {if:else}
            not divisible by 3: {test:number} <br />
        {/if}
    {/test}
1
  • 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
2

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'>"}
{switch="||</div>"}

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

1
  • 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.