-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: twig pagination #13
Comments
Can you explain what this does and how should this be used / how do you use it? |
Sure, you're welcome. I use this function to paginate random collections. In fact, it's little more than a thin wrapper for the existing paginate functionality in a slightly different context. The main point is that the code no longer depends on fixed page header settings and page initialization sequence. It can be called at a later time, e.g. in a twig template. First, there's a wrapper function in twigextension:
So I call this function from the twig template, feeding it a page collection and a limit. Specifically, in my application I create different views of a collection of pages (events in this case), the composition and size of which varies based on taxonomy or other parameters. Pagination should vary accordingly. The bulk of the pagination mechanism remains the same, thus the twig template should also contain the reference to pagination.html.twig, as per usual. Please note, that at present any existing pagination parameters for the collection are simply overwritten. This works fine for my application, but it should be possible to check for that and take some different action, if necessary. Also, I think disabling pagination may be necessary when using this function on a header collection, otherwise the collection is paginated twice, which may give unexpected results. More generally, one might think of some sensible way to handle already paginated collections. One possibility being to skip those altogether. More questions? Don't hesitate to ask. |
I'm still assuming this could be useful to others. This version changes the following things:
Default behaviour of pagination plugin unchanged, only the twig extension uses the parameters /*pagination.php*/
/**
* twig pagination
*
* @param collection $collection
* @param $limit
*/
public function paginateCollection( $collection, $limit, $ignore_url_params = false )
{
$collection->setParams(['pagination' => 'true']);
$collection->setParams(['limit' => $limit]);
$collection->setParams(['ignore_url_params' => $ignore_url_params]);
if ($collection->count() > $limit) {
require_once __DIR__ . '/classes/paginationhelper.php';
$this->pagination = new PaginationHelper($collection);
$collection->setParams(['pagination' => $this->pagination]);
$uri = $this->grav['uri'];
$start = ($uri->currentPage() - 1) * $limit;
if ($limit && $collection->count() > $limit) {
$collection->slice($start, $limit);
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
/* paginationhelper.php */
// get params
if (array_key_exists('ignore_url_params',$collection->params())) {
$url_params = ($collection->params()['ignore_url_params'])?
([]):(explode('/', ltrim($uri->params(), '/')));
} else {
$url_params = explode('/', ltrim($uri->params(), '/'));
}
--------------------------------------------------------------------------------------------------------------------------------------------
/* pagination/twig/PaginationTwigExtension.php */
namespace Grav\Plugin;
use \Grav\Common\Grav;
class PaginationTwigExtension extends \Twig_Extension
{
protected $config;
public function __construct()
{
$this->config = Grav::instance()['config'];
}
/**
* Returns extension name.
*
* @return string
*/
public function getName()
{
return 'PaginationTwigExtension';
}
/**
* Return a list of all functions.
*
* @return array
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('paginate', [$this, 'paginateFunc']),
];
}
public function paginateFunc($collection, $limit, $ignore_url_params) {
$pag = new PaginationPlugin($this, Grav::instance(), $this->config);
$pag->paginateCollection($collection, $limit, $ignore_url_params);
}
} |
@gigago Good change. Why not making a PR? |
Just made a pull request for this code. |
@gigago Can you give me a link? Can't see any PR in this repo. |
https://github.com/gigago/grav-plugin-pagination/pull/1 Sorry I’m a beginner in git. Pretty sure I made some mistakes.
|
You made a PR in your own plugin fork, but need to do this here ;) |
I suspected something like that. No problem, was a nice excercise, PR coming =)
|
@gigago This is such a nice addition … would be a shame to forget the PR ;) |
Was there ever a pull request created for this? |
This has been merged earlier this year: #22 |
So, this can be closed?! |
I do this with the following code in pagination.php. Hand-kludged from the existing code base, please improve whenever deemed necessary :-) At present, I call this from a twig simplefunc extension, worgs great! Maybe someone has a better proposal for integrating this functionality into grav.
The text was updated successfully, but these errors were encountered: