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.

"Switch" parameter help / solution?

January 15, 2011 6:03pm

Subscribe [5]
  • #1 / Jan 15, 2011 6:03pm

    maodiddy

    25 posts

    I have an issue with a solution I implemented to output entries in groups of four, using the switch parameter. Perhaps it’s not the best solution, so any advice on an alternate method to achieve the same result is most welcome.

    I have used the switch parameter to wrap every four entries with an opening and closing <li> tag, but there is one flaw in this approach.

    I have the following in the template:

    <ul class="slideshow listing plain">
            {exp:channel:entries channel="channel_portfolio" disable="member_data"}
                {switch="<li class='slide'>|||"}
                    <div class='item {switch="column1|column2"}'>{title}</div>
                {switch="|||</li>"}
            {/exp:channel:entries}
            </ul><!-- end slideshow -->

         

    This works great, except for when the total entries is not divisible by four. If there isn’t a fourth entry in each group, the <li> tag does not close.

    I can’t figure out how to ensure that there is always a closing </li> tag on the final group… Any ideas?

    Thanks so much!
    -m

  • #2 / Jan 17, 2011 12:54pm

    Tony Geer

    253 posts

    As far as I know, that’s not possible. You may need to redo it so that every entry is an li.

  • #3 / Jan 26, 2011 1:04pm

    jeremydouglas

    292 posts

    I think you would have to write a plugin or something for that.

    It would be really neat to have this. It would be handy for output slideshows when you want to show a few slides at once (for example using the jquery cycle plugin).

    What I have done in the past is use a bunch of if statements, like below:

    {exp:channel:entries channel=""}
                {if count==1 OR count==4 OR count==7 OR count==10 OR count==13 OR count==16 OR count==19 }
                <li class="group">
                {/if}
                    <span>{title}</span>                
                {if  count==3 OR count==6 OR count==9 OR count==12 OR count==15 OR count==18 OR count==total_results}</li> <!-- /group --> {/if}
                {/exp:channel:entries}
  • #4 / Dec 12, 2011 9:49pm

    johnHoysa

    79 posts

    Hmm sounds similar to what I just figured out the other week. I am using images so my code is a little different.

    {exp:channel:entries channel="my_channel"}
    
     {switch='<div class="newROW"><!-- OPEN newRow --><div class="nav_box"></div>||'}
     
      <div class="">
       <a href="#{url_title}">{project_image}</a>
      </div>
     
     {switch='||<div class="projectDetails"></div> </div><!-- Close newRowSwitch --> '}
    
    {/exp:channel:entries}
    
    <div class="projectDetails"></div> </div><!-- Close newRow if less then three images to show -->

    Notice my closing div out side of the EE tags? That extra line closes the div if less then three images are shown. The downside is that if there are three images showing then I close the same div twice. The code will not be W3C compliment but hey it works for those times where you have no idea how many entries there might be.

    If you know there will be only a few entries then use the code sample from above provided by jeremydouglas.

     

  • #5 / Feb 29, 2012 5:41am

    mrandmrsok

    1 posts

    Hi there, I’ve just faced a similar problem - trying to contain rows of three in a div and making sure the last div closes if the total number is not divisible by 3.
    Building on the php in this post http://ellislab.com/forums/viewreply/348264/ from Mark Bowen I’ve come up with this solution:

    {exp:channel:entries channel="channel"}
    {switch='<div class="group-of-three">||'}
    
    <h3>{title}</h3>
    <p>{description}</p>
    
    <p>{switch='||</div><!--/.group-of-three-->'}</p>
    
    <p>{if count == total_results}<br />
    <?php <br />
    if ({count}%3!=0){<br />
    echo "</div><!--/.group-of-three-->";<br />
    } <br />
    ?>{/if}<br />
    {/exp:channel:entries}

    Note the PHP at the end which checks whether, if it’s the last entry, it is divisible by three, and if not adds the closing div.
    Hope that’s useful!

     

  • #6 / Mar 16, 2012 9:06am

    TGP

    78 posts

    I think you would have to write a plugin or something for that.

    It would be really neat to have this. It would be handy for output slideshows when you want to show a few slides at once (for example using the jquery cycle plugin).

    What I have done in the past is use a bunch of if statements, like below:

    {exp:channel:entries channel=""}
                {if count==1 OR count==4 OR count==7 OR count==10 OR count==13 OR count==16 OR count==19 }
                <li class="group">
                {/if}
                    <span>{title}</span>                
                {if  count==3 OR count==6 OR count==9 OR count==12 OR count==15 OR count==18 OR count==total_results}</li> <!-- /group --> {/if}
                {/exp:channel:entries}

    I run into the same problem frequently and unfortunately this is what I end up doing.

     

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

ExpressionEngine News!

#eecms, #events, #releases