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.

Error with Pagination logic

April 06, 2011 6:52pm

Subscribe [3]
  • #1 / Apr 06, 2011 6:52pm

    dstechroom

    113 posts

    I’ve got an issue with EE2.x’s built-in pagination.  I’m using EE 2.1.3.

    I’m using the Previous and Next style of pagination on one of my blogs. My template code looks something like this:

    <h1>News</h1>
    
    <ul>
    
    {exp:channel:entries limit="4" paginate="bottom" channel="news"}
    
    <li class="{switch='left|right'}"><article>...</article></li>
    
    {paginate}
    
    {if {total_pages} > 1}
    </ul><div class="clear"></div><ul>
    {/if}
    
    {if next_page}
    <li class="pagination pagination_left"><a href="http://{auto_path}">OLDER POSTS</a></li>
    {/if}
    
    {if previous_page}
    <li class="pagination pagination_right"><a href="http://{auto_path}">NEWER POSTS</a></li>
    {/if}
    
    {/paginate}
    
    {/exp:channel:entries}
    
    </ul>

    The problem with the above code is that on the last page of posts, the NEWER POSTS link shows up in the left column instead of the right column.  On every other page where it is displayed, it shows up in the right column (with the OLDER POSTS link in the left column).

    The only way I can get it to show up in the right column when it is the only pagination link on the page (without some fancy CSS) is by outputting an empty <li class=“pagination pagination_left”></li> first before the pagination link LI.

    When I try adding an {if} block inside the {if previous_page} block, EE appears to have trouble interpreting the {if} block.  I’ll give a few examples of what I’m talking about.

    For example, if I modify the {if previous_page} block like this:

    {if previous_page}
    
    {if !next_page}<li class="pagination pagination_left"></li>{/if}
    
    <li class="pagination pagination_right"><a href="http://{auto_path}">NEWER POSTS</a></li>
    {/if}

    Then on the first page of posts I get this error:

    Parse error: syntax error, unexpected T_ENDIF in /[...]/system/expressionengine/libraries/Functions.php(650) : eval()‘d code on line 87

    And on every pagintion page (/P4, /P8, etc), the {if previous_page} block outputs nothing.

    Another example, if I modify the {if previous_page} block like this:

    {if previous_page}
    
    {if {current_page} == {total_pages}}<li class="pagination pagination_left"></li>{/if}
    
    <li class="pagination pagination_right"><a href="http://{auto_path}">NEWER POSTS</a></li>
    {/if}

    Then on the first page of posts I get this error:

    Parse error: syntax error, unexpected T_ENDIF in /[...]/system/expressionengine/libraries/Functions.php(650) : eval()‘d code on line 87

    And on every pagination page (/P4, /P8, etc), with the exception of the last page, the {if previous_page} block outputs nothing.  On the last pagination page, the button does get output, but the URL of NEWER POSTS link gets set to: http://example.com/news/{auto_path} instead of the correct pagination URL.

    (Not to mention my template only has 81 lines.)

    Based on these two examples, there appears to be a problem with how ExpressionEngine interprets {if} blocks when they are within the {paginate} tag’s {if next_page} and {if previous_page} tag blocks.

    Has anyone else come across this problem and/or have an idea how this could be fixed?

  • #2 / Apr 06, 2011 8:20pm

    Neil Evans

    1403 posts

    okay… not sure on how this all looks or how the HTML is rendered… but why not use a if else statement and put effectively a blank in place of the older posts html list item… i.e.

    {paginate}
    
    {if {total_pages} > 1}
    </ul><div class="clear"></div><ul>
    {/if}
    
    {if next_page}
    <li class="pagination pagination_left"><a href="http://{auto_path}">OLDER POSTS</a></li>
    {if:else}
    <li class="not_pagination pagination_left"> </li>
    {/if}
    
    {if previous_page}
    <li class="pagination pagination_right"><a href="http://{auto_path}">NEWER POSTS</a></li>
    {if:else}
    <li class="not_pagination pagination_right"> </li>
    {/if}
    {/paginate}

    Also in terms of your logic, querying EE and various variables… i often hit issues with logic.
    Typically i find that i can do a simple check if soehting is there in the format of:

    {if my_field_name}

    But if it is more complicated or a comparison i end up doing:

    {if "{my_field_name}"!="some-string"}

    Any of that help?

  • #3 / Apr 07, 2011 11:25am

    dstechroom

    113 posts

    When I try adding the if:else blocks as in your example, I get the following PHP error when I try loading the page:

    Parse error: syntax error, unexpected T_ELSE in /.../system/expressionengine/libraries/Functions.php(650) : eval()‘d code on line 81

    (Line 81 is empty)


    In /system/expressionengine/modules/channel/mod.channel.php starting around line 878, it looks like the {if next_page} and {if previous_page} blocks are parsed differently than standard {if} blocks—these are actually parsed like {field_name}{/field_name} tags instead of standard {if} blocks.


    So that means that if I want to use an {if:else} case, I need to not use {if next_page} and {if previous_page} blocks—I need to determine cases where next_page and previous_page would be true or false and write logic based on that.

  • #4 / Apr 07, 2011 11:27am

    dstechroom

    113 posts

    This is why I get errors when I try nesting an {if} inside it or if I use an {if:else}.  The regular expression checks for the first instance of {/if} and uses that as the end of the {if next_page} or {if previous_page} blocks.—Meaning that any {if:else} or {/if} that appears after the nested {/if} throws an error—because those tags would technically not be tied to known {if} tag.

  • #5 / Apr 07, 2011 11:56am

    Neil Evans

    1403 posts

    Hey,
    Looking at the userguide for the pagination - see what your saying.
    http://ellislab.com/expressionengine/user-guide/modules/channel/pagination_page.html#var_if_next_page

    {if next_page}

    is not actually a standard if statement, but a special tag pair used for this function. Hence it seems that else, and other if logic will not work as this is just not a normal if. Confusing and odd to say the least!

    Might be worth a quick look at devot-ee:
    http://devot-ee.com/add-ons/advanced-prev-next-entry/
    http://devot-ee.com/add-ons/next-previous-links-anywhere/

    Not sure they meet your needs, but seem in the right ball park at least!

  • #6 / Apr 07, 2011 11:57am

    dstechroom

    113 posts

    With this new information I’ve changed up the {paginate} section as so:

    {paginate}
    
    {if {total_pages} > 1}
    </ul><div class="clear"></div><ul>
    {/if}
    
    {!-- the last page has no OLDER link --}
    {if {current_page} == {total_pages}}
    
    <li class="pagination pagination_left"><span></span></li>
    
    {if:else}
    
    <li class="pagination pagination_left">
    {if next_page}<a href="http://{auto_path}"><span>OLDER POSTS</span></a>{/if}
    </li>
    
    {/if}
    
    {!-- the first page has no NEWER link --}
    {if {current_page} == 1}
    
    <li class="pagination pagination_right"><span></span></li>
    
    {if:else}
    
    <li class="pagination pagination_right">
    {if previous_page}<a href="http://{auto_path}"><span>NEWER POSTS</span></a>{/if}
    </li>
    
    {/if}
    
    {/paginate}

    I’m still using the {if next_page} and {if previous_page} tags but instead of trying to use them for logic, I’m just using them for the {auto_path} variable.  All the logic for determining whether the link gets output is determined by the {if}{if:else}{/if} blocks they are nested in.

    Problem solved.

  • #7 / Apr 07, 2011 3:07pm

    Sue Crocker

    26054 posts

    Yay for Nevsie helping out.

    Glad things are working for you again, dstechroom. Feel free to start a new thread if you have any more questions.

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

ExpressionEngine News!

#eecms, #events, #releases