4

I've got a Matrix field with say 20 rows (could be 21).

I want to output those rows within but i want 2 rows in each .

Any idea how to accomplish this with the switch tag in Matrix? I'm trying some weird and wacky stuff but nothing is working as i want.

So markup i would want is:


<div>
{row 1 content}
{row 2 content}
</div>

<div>
{row 3 content}
{row 4 content}
</div>

<div>
{row 5 content}
{row 6 content}
</div>

<div>
{row 7 content}
</div>

And here is my current template code:

{exp:channel:entries channel="downloads" dynamic="no" disable="pagination|member_data|categories|category_fields" orderby="random"}
    <div>
{dl-downloads}

        <h4>{dl-download-title}</h4>
        <p>{dl-download-desc}</p>
        {switch="|</div><div>"}

{/dl-downloads}
    </div>
{/exp:channel:entries}
3

The Matrix {switch} variable will work for this. Try:

<div>
    {matrix_field}
        {row_count}<br>
        {switch="|</div><div>"}
    {/matrix_field}
</div>

Which will output:

<div>
    1
    2
</div>
<div>
    3
    4
</div>
<div>
    5
    6
</div>
<div>
    7
</div>

Just replace {row_count} in my example with whatever Matrix column short names you would want to use.

5
  • Hmm, no, that's not working. Still getting one <div>{stuff}</div> for each row. It's not doing it for 2 rows. Mar 5 '13 at 17:37
  • Strange. It works fine in my test template. Can you post your template code? Or maybe a simplified test case that isn't working for you?
    – Alex Kendrick
    Mar 5 '13 at 17:43
  • I've edited the original question with my current template code based on your suggestion. Thanks! Mar 5 '13 at 17:47
  • 1
    Oh goodness me, i just realised, there are multiple channel entries with one matrix row in each, not the other way round! Your code works fine in the channel loop! What a idiot i am!! Cheers Alex!! Mar 5 '13 at 17:50
  • Glad you got it sorted! Funny how sometimes just posting template code can make you realize the answer.
    – Alex Kendrick
    Mar 5 '13 at 17:53
0

Not sure I get it. You want to output all your rows and wrap all sets of two rows with a div?

Will something like this do the trick, or am I mising something?

{matrix}
    {if "{switch='one|two'}" == "one"}<div class="row">{/if}
    ... something ...
    {if "{switch='one|two'}" == "two" || "{row_count}" == "{total_rows}"}</div>{/if}
{/matrix}
2
  • I've added a better code example for you in the initial question :) Mar 5 '13 at 17:12
  • Yep my solution would work as well for what you are trying to accomplish. I tend to find it more readable but that's just me. Mar 7 '13 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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