ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How can I put every 2 entries into its own DIV container?

September 05, 2012 3:09am

Subscribe [1]
  • #1 / Sep 05, 2012 3:09am

    Danny T.

    426 posts

    Thought this one would come easy, but it’s got me absolutely stumped. Can anybody advise on how I can put every 2 entries into its own DIV container?

    Looking to output something similar to this:

    <div class="entry">
    <h2>{title}</h2>
    <h2>{title}</h2>
    <p></div></p>
    
    <p>then repeats every 2..</p>
    
    <p><div class="entry"></p><h2>{title}</h2>
    <h2>{title}</h2>
    <p></div>

  • #2 / Sep 05, 2012 7:17am

    John St-Amand

    865 posts

    Switch variable to the rescue:

    {exp:channel:entries channel="whatever"}
    {switch="<div class='entry'>|"}
    <h2>{title}</h2>
    <p>{if count != total_results}{switch="|</div>"}{/if}<br />
    {if count == total_results}</div>{/if}<br />
    {/exp:channel:entries}

    Note the single quotes around the class in the opening div because it’s within the switch - only way switch plays nice.  And the reason for the two distinct (though still simple) conditionals at the end is to ensure that the div gets closed, even if your loop loads an uneven number of entries.

    Hope that helps.

  • #3 / Sep 05, 2012 2:19pm

    Danny T.

    426 posts

    Hi John! Glad I inquired about it! Many thanks for the help. Had a thought I could use the switch operator somehow, but my previous experience was to only use it to switch CSS classes.

    Many thanks! 😊

  • #4 / Sep 05, 2012 3:42pm

    John St-Amand

    865 posts

    Yeah, in the documentation it kind of leans in the CSS direction, but the reality is it’s really just a repeating pattern “injector” or sorts, and if you get creative with it, it can be really powerful.  Glad I could help.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases