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.

Conditional to see if paginate is needed?

November 07, 2012 9:45am

Subscribe [2]
  • #1 / Nov 07, 2012 9:45am

    John Morton

    84 posts

    I need to conditionally show include some HTML in a template when a page doesn’t require pagination, ie, on a page where the paginate loop won’t render with the channel:entries loop.

    It might be easiest to show with an example. Here is basically what I’m trying to accomplish.

    {exp:channel:entries channel="blog_entries" orderby="date" sort="desc" limit='15' paginate='top' disable="member_data|trackbacks"}
    
    {paginate}
     {if next_page}<a href="http://{auto_path}" class='next'>Next ></a>{/if}
     {if previous_page}<a href="http://{auto_path}" class='prev'>< Previous</a>{/if}
    {/paginate}
    
    <!-- An example conditional, SUDO code that does not work, to show 
            when EE loop does not need to render the paginate loop. -->
    
    {if pagination_needed=='no' }
     There is no need to display pagination on this page since all the entries fit on a single page.
    {/if}
    
    <!-- End of fake SUDO code block. -->
    
    <article>
     <h2><a href="http://{url_title_path=%22blog/entry%22}" title="Read: {title}">{title}</a></h2>
    <p> {excerpt}<br />
    </article></p>
    
    <p>{/exp:channel:entries}

    Is there a native way to check to see if the pagination code will render so I can render an alternate loop in it’s spot instead?

  • #2 / Nov 07, 2012 9:54am

    glenndavisgroup

    436 posts

    Hi John,

    Would something like this work for you:

    {paginate}
    {if {total_pages} > 1}
      ...your code goes here…
    {/if}
    {/paginate}

    Mike

  • #3 / Nov 07, 2012 10:12am

    John Morton

    84 posts

    Thanks for the reply, Mike. I don’t think so because the conditional I’m looking for is for when the content within the paginate tags won’t render.

    I’ve tried testing for the {total_pages} being equal to 1, but that doesn’t seem to get parsed.

    {paginate}
    {if {total_pages} == 1}
      ...never renders because paginate loop doesn't execute when there is no need to show pagination
    {/if}
    {/paginate}

    I’m wanting to display something when paginate doesn’t display.

    If I could access {total_pages} outside the paginate loop, I could get that to work though, so I think you’re on the right track.

  • #4 / Nov 07, 2012 10:21am

    glenndavisgroup

    436 posts

    What about changing it around like so:

    {if {total_pages} > 1}
    {paginate}
    ...
    {/paginate}
    {if:else}
    ...
    {/if}

    Mike

  • #5 / Nov 07, 2012 10:27am

    John Morton

    84 posts

    Also, I’ve read the old bugrtracker entry from here:

    https://support.ellislab.com/bugs/detail/11298/

    If I read it correctly, the paginate tag at one point did what I’m expecting it to do but it doesn’t seem to be working that way for me now on EE 2.5.3.

  • #6 / Nov 07, 2012 10:29am

    John Morton

    84 posts

    Hi Mike.

    It doesn’t appear that {total_pages} renders outside the {paginate} loop unfortunately.

    -John

  • #7 / Nov 08, 2012 9:50am

    John Morton

    84 posts

    I still don’t have a solution for this issue. Anyone have additional thoughts on it?

  • #8 / Nov 08, 2012 12:41pm

    Dan Decker

    7338 posts

    Hi John,

    You’ll probably have some luck checking your {total_results} against your limit= value:

    {if total_results <= '15'}
    then no pagination
    {/if}

    Does that feel like a workable solution to you?

    Cheers,

  • #9 / Nov 08, 2012 1:00pm

    John Morton

    84 posts

    No, it’s still not quite what I need. I need to know whether the pagination block is going to render or not.

    The solution you suggest makes that block of “then no pagination” text appear on every page except the last page, assuming the last page doesn’t happen to have exactly the same number of entries as the limit.

    The {paginate} tag pair used to render all the time and you’d put some logic inside it to show or not show ‘next’ or ‘prev’ when needed. That’s the scenario that this bug report, https://support.ellislab.com/bugs/detail/11298/, seems to suggest. I think the bug report’s name “paginate tagdata should only be placed if there are multiple pages” may have been taken as a true bug, ie, not displaying anything in the paginate tag pair when pagination links aren’t needed by the channel:entries tag. I need to show alternate content in the situation when the paginate stuff isn’t present, but only then.

  • #10 / Nov 08, 2012 2:52pm

    glenndavisgroup

    436 posts

    Lets try using PHP and do something like this:

    <?php $blnPaginate = false;?>
    {exp:channel:entries channel="blog_entries" orderby="date" sort="desc" limit="15" paginate="top" disable="member_data|trackbacks"}
    
    {paginate}
    <?php $blnPaginate = true;?>
    {/paginate}
    
    {/exp:channel:entries}
    
    {exp:channel:entries channel="blog_entries" orderby="date" sort="desc" limit="15" paginate="top" disable="member_data|trackbacks"}
    
    {paginate}
     {if next_page}<a href="http://{auto_path}" class='next'>Next ></a>{/if}
     {if previous_page}<a href="http://{auto_path}" class='prev'>< Previous</a>{/if}
    {/paginate}
    
    <!-- An example conditional, SUDO code that does not work, to show 
            when EE loop does not need to render the paginate loop. -->
    
    {if '<?php echo $blnPaginate;>'=='false' }
     There is no need to display pagination on this page since all the entries fit on a single page.
    {/if}
    
    <!-- End of fake SUDO code block. -->
    
    <article>
     <h2><a href="http://{url_title_path=%22blog/entry%22}" title="Read: {title}">{title}</a></h2>
    <p> {excerpt}<br />
    </article></p>
    
    <p>{/exp:channel:entries}

    I hope that helps.

    Mike

  • #11 / Nov 08, 2012 4:25pm

    John Morton

    84 posts

    Hi Mike,

    That looks like it will work. Thanks for the tips!

    My next stop was to look for a hook in the channel module and build an extension which my deadline doesn’t really allow for on this one.

    Cheers,

    John

  • #12 / Nov 08, 2012 5:15pm

    Shane Eckert

    7174 posts

    Hey John,

    I am glad that all is well! How awesome is the ExpressionEngine community?

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases