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.

break tag loop

September 23, 2011 9:57am

Subscribe [4]
  • #1 / Sep 23, 2011 9:57am

    neuralynx

    62 posts

    I can’t imagine this hasn’t been discussed in the past, but I could not find anything via search—apologies.

    Is there any construct to break out of a tag-pair loop? Something like this:

    {exp:channel:entries}
      {if count > 5}
        {break}
      {/if}
    {/exp:channel}
  • #2 / Sep 23, 2011 5:19pm

    Mark Bowen

    12637 posts

    Hi neuralynx,

    There isn’t anything like that but you could use the limit=“5” parameter on the Channel Entries Tag in order to get the same sort of thing.

    Does that help at all?

    Thanks,

    Mark

  • #3 / Sep 26, 2011 12:45pm

    neuralynx

    62 posts

    Mark,

    I know about the ‘limit’ parameter, but it unfortunately affects the {absolute_results} tag.  I am trying to accomplish the fairly simple task of setting a couple variables to the number of entries in a channel.  To do this, I am using:

    {exp:channel:entries channel="products" category="{embed:category_id}"}
     {categories}
       <?php 
        $catName = '{category_name}';
        $numItems = '{absolute_results}';
       ?>
      {/categories}
    {/exp:channel:entries}

    This loop only needs to run once, but it might loop 100 times if I have 100 products in the channel with category “1” (for example).

    I can’t use a limit param since I need to set the number of results.  Is there a better way to achieve the end result in a more efficient manner?

     

  • #4 / Sep 27, 2011 2:15am

    John Henry Donovan

    12339 posts

    Hi neuralynx,

    I can’t use a limit param since I need to set the number of results.

    Can you put this in context please? What are you trying to show as a final result? The number of entries per category?

    Note you also have the {total_results} variable

  • #5 / Sep 27, 2011 10:00am

    neuralynx

    62 posts

    The {total_results} tag is also affected by the limit parameter.

    The context is in displaying product thumbnails at the top of a page.  If there are less than 7 products in a channel, I want to just display them all in a simple row.  If there are more than 6 products, I want to create a carousel and display them that way.  Also, for a simple row, I need to know the number of products so that I can set the width of the container in my css to allow for centering.

    I imagine this need has come up a lot.  I can think of many instances where one would like to know the number of entries in a channel, category, etc, without having to place the tag pair and loop through all the entries.

  • #6 / Sep 28, 2011 11:23am

    Mark Bowen

    12637 posts

    Hi neuralynx,

    If you are using custom PHP in your templates then I’ll move this post over to the Community Help forums so that others might be able to help you out on this as it’s beyond the scope of support.

    That said this code might help you out here :

    {exp:channel:entries
     channel="default_site"
     disable="member_data|trackbacks|categories|category_fields|pagination"}
    
    {if total_results <= "6"}
    Place all your repeating code here for showing the six items in a row
    {/if}
    
    {if total_results > "6"}
    Place your repeating code here for your carousel
    {/if}
    
    {/exp:channel:entries}

    Does that help at all?

    Thanks,

    Mark

     

  • #7 / Sep 28, 2011 2:06pm

    neuralynx

    62 posts

    Mark,

    The code you present above was pretty much my original idea.  Correct me if I’m wrong though, but I believe the loop will run, and the {if} statements will be executed for every entry in the channel.  If I have 100 entries, both statements get tested 100 times, and my carousel gets created 100 times.

    Also, I wouldn’t really classify this template as custom PHP—the only reason I am using PHP in this case is because I need to set some variables, and there is no way to do that with EE tags.

  • #8 / Sep 29, 2011 12:46pm

    Mark Bowen

    12637 posts

    Hi neuralynx,

    No only one of the {if} statements will run depending on the outcome of the conditional.

    You can easily test this by going to Admin > System Administration > Output and Debugging and turning on Display Output Profiler.

    If you place this code in your template and nothing else :

    {exp:channel:entries
     channel="default_site"
     disable="member_data|trackbacks|categories|category_fields|pagination"}
    
    Place all your repeating code here for showing the six items in a row
    
    {/exp:channel:entries}

    Take a look at the output profiler to see how many queries are produced when you run that then change the code for the code I posted above (shown again here below) :

    {exp:channel:entries
     channel="default_site"
     disable="member_data|trackbacks|categories|category_fields|pagination"}
    
    {if total_results <= "6"}
    Place all your repeating code here for showing the six items in a row
    {/if}
    
    {if total_results > "6"}
    Place your repeating code here for your carousel
    {/if}
    
    {/exp:channel:entries}

    Note how many queries it uses this time. They should be the same hopefully.

    Does that help?

    Thanks,

    Mark

     

  • #9 / Sep 29, 2011 1:47pm

    neuralynx

    62 posts

    No, that’s not the way it works.  I have this actual test code running right now to illustrate:

    {exp:query sql="SELECT cat_id FROM exp_categories WHERE cat_url_title = '{segment_3}'"}
    
     {exp:channel:entries
      channel="products"
      category="{cat_id}"
      disable="member_data|trackbacks|categories|category_fields|pagination"}
    
     <h1>Running the channel loop</h1>
     
     {if total_results <= "6"}
     <table>
      <tr><th>Results less than 6</th></tr>
      <tr><td>Total Results = {total_results}</td></tr>
      <tr><td>Absolute Results = {absolute_results}</td></tr>
     </table>
     {/if}
    
     {if total_results > "6"}
     <table>
      <tr><th>Results greater than 6</th></tr>
      <tr><td>Total Results = {total_results}</td></tr>
      <tr><td>Absolute Results = {absolute_results}</td></tr>
     </table>
     {/if}
    
     {/exp:channel:entries}  
    
    {/exp:query}

    With a product category of adapters, there are 9 items, and it runs the loop 9 times, tests the conditionals 9 times, and would create 9 product rows if I had all that code in place.  The interesting thing is that {total_results} is always 1, while {absolute_results} is always 9.  Try it yourself at http://dev.neuralynx.com/products/test/adapters/

    It works the same for a product channel with only 2 items: http://dev.neuralynx.com/products/test/electrode_interface_boards/

     

  • #10 / Oct 02, 2011 5:39pm

    Sue Crocker

    26054 posts

    Did you notice you are specifying a category, then turning off categories?

    Just caught my eye…

  • #11 / Oct 03, 2011 1:12pm

    neuralynx

    62 posts

    Did you notice you are specifying a category, then turning off categories?

    Yes, I copied that “disable” parameter from Mark’s example.  For the example, I don’t think it matters, since the example did not use the categories.  However, I do need to select the channel entries by category, since I need to know how many entries there are for a specific category.

    Bottom line: Since there is no way to break out of a channel tag pair loop, is there anyway to simply get the total number of channel entries for a given category without looping through all the entries?

  • #12 / Oct 04, 2011 12:50pm

    Mark Bowen

    12637 posts

    Hi neuralynx / Sue,

    Sorry about the disable=“categories” code above. That was me being lazy and using a TextExpander command to quickly place in a Channel Entries tag there 😉

    When you say “without looping through all the entries” then if you want to do this sort of thing you can use this code :

    {if count == "1"}
    …output count here…
    {/if}

    By placing {if count == "1"}content here{/if} inside of your Channel Entries tag then anything within that conditional will only get run the once.

    You will however of course still be having the tag check for all entries so this obviously isn’t the ideal solution.

    You could therefore either use a custom SQL query to bring that number back or perhaps a 3rd party add-on in order to do the same sort of thing.

    Does that help at all?

    Thanks,

    Mark

     

  • #13 / Oct 04, 2011 4:05pm

    neuralynx

    62 posts

    Mark,

    Thanks for the {if count=="1"} hint.  That at least keeps the conditional checks to a minimum.

    Okay, thanks for everyone’s help.  The original question was whether there was any sort of break construct, and the answer is no.

    There is so much to like about EE—however, even at version 2.2, there is a lot of seemingly basic functionality that is missing, or not quite working as expected.  I’m sure there are a roomful of developers chained to their desks, working on requested features and fixing bugs as fast as they can.  Buy ‘em a round of Red Bull on me!:-)

    We’ve beat this one enough…

  • #14 / Oct 06, 2011 11:20am

    Mark Bowen

    12637 posts

    Hi neuralynx,

    Glad this has got you sorted now. If you’d like to put in a Feature Request to have something like a break put in then please feel free to do so.

    If anything else crops up then please don’t hesitate to post again as needed.

    Cheers,

    Mark

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

ExpressionEngine News!

#eecms, #events, #releases