Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
2 posts Page 1 of 1
Recently I had a, somewhat unusual, request from a member -
he was using <cms:pages> loop to display his gallery items in a paginated manner and wanted the first page to show 10 items and the rest to show 50.

Prima-facie, it appears that we can do this easily by setting the 'limit' parameter of cms:pages to '10' on the first page and 50 on the rest but this approach won't work for the following reason -

The first page will display 10 pages correctly but when we get to the second (or the other pages) and use 50 as the new limit, cms:pages does display 50 pages *but* it assumes that the previous page(s) also had the same value for 'limit' (i.e. 50) so skips 50 pages and begins with 51. This leads to 40 pages being skipped on the second page as the correct behavior would have been to start listing from the 11th page.

This use-case necessitated the addition of a new parameter to cms:pages named 'adjust' (current version from GitHub has it).
Using this parameter we can explicitly ask cms:pages to tweak the value it uses to start its listing from.

For example, in the use case under discussion, we need to tell Couch that there is a difference of -40 (difference between limit on first page and limit on the other pages) pages. So for all pages other than 1, following is the code we can use -
Code: Select all
<cms:pages masterpage='gallery.php' paginate='1' limit='50' adjust='-40'> 

Had the use-case dictated that the first page show 50 pages and the rest 10, the adjust factor would be 40 (positive). In that case, the code for all pages other than first would become -
Code: Select all
<cms:pages masterpage='gallery.php' paginate='1' limit='10' adjust='40'> 

In case someone finds himself having to implement this kind of 'non-uniform' pagination, following is some generic code that can help in making the calculation easier.
Just before the <cms:pages> block doing the listing, place the following code
Code: Select all
<!-- set only these two numbers -->
<cms:set my_limit_for_first_page = '10' />
<cms:set my_limit_for_all_other_pages = '50' />

<cms:set my_pg="<cms:gpc 'pg' method='get' default='1' />" />
<cms:if my_pg eq '1' >
    <cms:set my_limit=my_limit_for_first_page />
<cms:else />
    <cms:set my_limit=my_limit_for_all_other_pages />
    <cms:set my_adjust="<cms:sub my_limit_for_first_page my_limit_for_all_other_pages />" />
</cms:if>

You need to tweak only the first two lines to put the desired numbers.

And now add the 'limit' and 'adjust' parameters to your <cms:pages> e.g. as follows -
Code: Select all
<cms:pages masterpage='gallery.php' paginate='1' limit=my_limit adjust=my_adjust>

That will take care of specifying the correct values for the limit and adjust parameters.

Test out and the first page should show first 10 pages and the second the next 50.

There would be one problem at this point however.
When on the first page, since the limit is 10, the paginator will show a higher number of pages than the inner pages where the limit is 50 (e.g. if total number of pages is 200, the first page will show 20 crumbs while the inner will only have 4).

This can be fixed by making the first page explicitly use a paginator with a higher limit as follows.
Suppose your original code has the following for the paginator -
Code: Select all
<cms:pages ..

    ..
   
    <cms:if k_paginated_bottom >
        <cms:paginator /><br>
    </cms:if>
</cms:pages>

Please modify it as follows to put in the check -
Code: Select all
<cms:pages ..

    ..
   
    <cms:if k_paginated_bottom >
        <cms:if k_current_page eq '1' >
            <cms:set my_limit="<cms:if my_limit_for_first_page gt my_limit_for_all_other_pages><cms:show my_limit_for_first_page /><cms:else/><cms:show my_limit_for_all_other_pages /></cms:if>" />

            <cms:pages masterpage=k_template_name paginate='1' limit=my_limit skip_custom_fields='1'>
                <cms:paginator /><br>
            </cms:pages>
        <cms:else />   
            <cms:paginator /><br>
        </cms:if>
    </cms:if>
</cms:pages>

Hope this helps.
KK wrote: consider what happens if we wish to start the listing skipping 3 posts (perhaps sticky) *and* the first page shows 10 while the others show 50.
Not an implausible situation. We now have to use both 'offset' and 'adjust' in this case.


Wow, it makes perfect sense with both adjust and offset. Great solution!
2 posts Page 1 of 1

Who is online

In total there are 5 users online :: 0 registered, 0 hidden and 5 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 5 guests

cron