Sourcefabric Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Newscoop 4.1 Cookbook

Pagination of article lists

Pagination means dividing content into discrete pages, each displaying a page number. When a long list of search results is divided into more than one page, allowing the reader to proceed from page to page, that's pagination.

This chapter will introduce an easy way for pagination in the beginning. If you run a larger site, you should use the method explained further down in this chapter.

Pagination of a long article list

Usually you might want to split a long list of articles into pages. You might do this on Section pages to show all articles in that particular section, without requiring the reader to scroll down too much.

The simplest pagination with "next" and "previous" buttons will look like this:

{{list_articles length="10"}}
...

<!-- we want to have pagination at the end of article list -->
{{if $gimme->current_list->at_end}}
      <!-- let's make sure that there are more articles-->
      {{ if $gimme->current_list->count > $gimme->current_list->length }}
          {{ if $gimme->current_list->has_previous_elements }}
              <a href="{{ url options="previous_items" }}" title="">previous</a>
          {{/if}}
 
          {{ if $gimme->current_list->has_next_elements }}
             <a href="{{ url options="next_items" }}" title="">next</a>
          {{/if}}
      {{/if}}
   {{/if}}
{{/list_articles}}

Pagination with page numbers

To get this kind of pagination we need to dig into smarty code a little bit more. Please read the code. It is clear enough to get general idea how it works.

{{list_articles length="10"}}
...
<!-- we want to have pagination at the end of article list -->
{{if $gimme->current_list->at_end}}
{{ if $gimme->current_list->count > $gimme->current_list->length }}
{{ $page=intval($gimme->url->get_parameter($gimme->current_list_id())) }}
{{ $list_id=$gimme->current_list_id() }}
<!-- sets the length of article list -->
{{$listLength=10}}
{{assign var="allPages" value=($gimme->current_list->count/$listLength)|ceil }}
{{assign var="currentPage" value=$page/$listLength}}
{{assign var="firstToShow" value=$currentPage-2}}
{{assign var="lastToShow" value=$currentPage+5}}
{{if $firstToShow < 1 }}
{{assign var="firstToShow" value=1}}
{{assign var="lastToShow" value=$lastToShow+3}}
{{/if}}
{{if $lastToShow > $allPages }}
{{assign var="lastToShow" value=$allPages+1}}
{{/if}}
<nav class="bottom_nav pagination">
<!-- we are unsetting article to make sure that is not going to affect url -->
{{ unset_article }}
{{ if $gimme->current_list->has_previous_elements }}
<a href="{{ url options="previous_items" }}" class="float_left nav_button prev" title="">previous
</a>
{{/if}}
{{if $lastToShow-$firstToShow>0}}
<div class="numbers">
<ul>
{{if $firstToShow>1}}
<li class="firstlast"><a href="{{ url options="section" }}{{if $gimme->topic->identifier}}?tpid={{$gimme->topic->identifier}}{{/if}}">1</a></li>
<li class="firstlast">...</li>
{{/if}}
{{section name=foo start=$firstToShow loop=$lastToShow}}
{{if $smarty.section.foo.index-1==$currentPage}}
<li class="current">{{ $smarty.section.foo.index }}</li>
{{else}}
<li><a href="{{ url options="section" }}?{{$list_id}}={{ ($smarty.section.foo.index-1)*$listLength }}{{if $gimme->topic->identifier}}&tpid={{$gimme->topic->identifier}}{{/if}}">
{{ $smarty.section.foo.index }} </a></li>
{{/if}}
{{/section}}
{{if $lastToShow-1<$allPages}}
<li class="firstlast">...</li>
<li class="firstlast"><a href="{{ url options="section" }}?{{$list_id}}={{ ($allPages-1)*$listLength }}{{if $gimme->topic->identifier}}&tpid={{$gimme->topic->identifier}}{{/if}}">{{$allPages}}</a></li>
{{/if}}
</ul>
</div>
{{/if}}
{{ if $gimme->current_list->has_next_elements }}
<a href="{{ url options="next_items" }}" class="float_right nav_button next" title="">{{ if $gimme->language->english_name == "Georgian" }}შემდეგი{{elseif $gimme->language->english_name == "Russian"}}следующий{{else}}next{{/if}} &raquo;</a>
{{ /if }}
</nav>
{{ /if }}
{{/if}}
{{/list_articles}}

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.