-
Notifications
You must be signed in to change notification settings - Fork 0
Pagination
The Query
snippet ties into the Craftsman Coding Pagination Library. Before pagination links can be rendered, you must do two things:
- You need to set a
&_limit
value: this will define the number of results you want to show per page - Your query must return more than than number of results (i.e. you need to have multiple pages of results)
If you haven't defined a limit (i.e. the number of results you want displayed on each page), then all results will be shown on a single page. Or if you have specified a limit, but you don't have enough rows in your search results, then there is no need for pagination.
The easiest way to change the appearance of the links generated by the Query
Snippet is to use one of the built-in
styles by setting the &_style
argument:
[[Query? &_style=`flickr`]]
More commonly, however, you will also be specifying other formatting parameters and you must dynamically listen for the
offset parameter in your URLs. Make sure you include the [[+pagination_links]]
placeholder in the &_tplOuter
or
somewhere on your page. A more complete example might look like this:
[[!Query? &_limit=10 &_style=`apple`
&_tpl=`<li><a href="[[~[[+id]]]]">[[+pagetitle]]</a></li>`
&_tplOuter=`<ul>[[+content]]</ul>[[+pagination_links ]]`
&_offset=`offset:get`]]
Short and sweet with no embedded styles. Start here if you want to use your own CSS.
Tastes like Apple.
Dig it.
See the Pagination Library for the latest supported styles.
Formatting can be complicated because it involves a handful of interrelated formatting strings.
Here is a sample representation of some pagination links:
<<First <<Prev 1 2 3 Next>> Last>>
\_____/ \____/ ^ ^ ^ \____/ \____/
| | | | | | +----- lastTpl
| | | | | +------------ nextTpl
| | | | +----------------- currentPageTpl
| | +-+------------------- pageTpl
| +------------------------- prevTpl
+--------------------------------- firstTpl
\_________________________________________________/
|
+-------------------- outerTpl
You can copy the pagination config file from core/components/query/config/default.config.php
firstTpl : formats a link that jumps to the first page of results.
<a href="[[~[[*id]]? &offset=`0`]]">« First</a>
lastTpl : formats the link that jumps to the last page of results.
<a href="[[~[[*id]]? &offset=`[[+offset]]`]]">Last »</a>
prevTpl : formats a link that jumps to the previous page of results.
<a href="[[~[[*id]]? &offset=`[[+offset]]`]]">‹ Prev.</a>
nextTpl : formats the link that jumps to the next page of results.
<a href="[[~[[*id]]? &offset=`[[+offset]]`]]">Next ›</a>
currentPageTpl : formats the indicator of the current page (usually not a link).
<span>[[+page_number]]</span>
pageTpl : formats a link to a specific page of results.
<a href="[[~[[*id]]? &offset=`[[+offset]]`]]">[[+page_number]]</a>
outerTpl : formats the HTML that wraps the entire generated set of links.
<div id="pagination">[[+content]]<br/>
Page [[+current_page]] of [[+page_count]]<br/>
Displaying records [[+first_record]] thru [[+last_record]] of [[+record_count]]
</div>
When pagination links are created, the following placeholders are set:
- extra
- base_url e.g. "/my/page?"
- offset = 0
- results_per_page 5
- page_count
- current_page
- lowest_visible_page : page number of first visible page
- highest_visible_page : page number of the last visible page
- first_record : first record on the current page
- last_record : last record on the current page
- record_count : total number of pages
- link_cnt : max number of page links
- content : used in the outerTpl to contain the links
- pagination_links : the final result
Here's a sample query on a custom data set:
[[!Query?
&status=`approved`
&_pkg=`reviews;[[++core_path]]components/reviews/model/;axpr_`
&_classname=`Reviews`
&_sortby=`created`
&_sortdir=`DESC`
&_tpl=`reviewTpl`
&_limit=`5`
&_offset=`offset:get`
&_config=`myconfig`]]
We're using a custom config to format the pagination links. Here's what it looks like:
<?php
return array (
'firstTpl' => '',
'lastTpl' => '',
'prevTpl' => '<a href="[[~[[*id]]? &offset=`[[+offset]]`]]">< Prev.</a> ',
'nextTpl' => ' <a href="[[~[[*id]]? &offset=`[[+offset]]`]]">Next. ›</a>',
'currentPageTpl'=> ' <span>[[+page_number]]</span> ',
'pageTpl' => '',
'outerTpl' => [[+content]]
Displaying [[+first_record]] thru [[+last_record]] of [[+record_count]] reviews',
);
?>
NOTE: that the _offset
parameter uses an input filter to read the offset parameter out of the generated URLs.