Skip to content
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

Closed
gigago opened this issue Mar 6, 2016 · 13 comments
Closed

feature request: twig pagination #13

gigago opened this issue Mar 6, 2016 · 13 comments

Comments

@gigago
Copy link
Contributor

gigago commented Mar 6, 2016

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.

use Grav\Common\Uri;

  /**
     * 'runtime' pagination
     *
     * @param collection $collection
     * @param $limit
     */
    public function paginateCollection(Collection $collection, $limit)
    {
        $collection->setParams(['pagination' => 'true']);
        $collection->setParams(['limit' => $limit]);

        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);
            }
        }
    }
@flaviocopes
Copy link
Contributor

Can you explain what this does and how should this be used / how do you use it?

@gigago
Copy link
Contributor Author

gigago commented Mar 6, 2016

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:

new \Twig_SimpleFunction('paginate', [$this, 'paginateFunc'])
...
public function paginateFunc($collection,$limit) {
        $pag = new \Grav\Plugin\PaginationPlugin($this, $this->grav, $this->config);
        $pag->paginateCollection($collection,$limit);
    }

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.

@gigago gigago changed the title feature request: add 'runtime' pagination feature request: twig pagination Mar 14, 2016
@gigago
Copy link
Contributor Author

gigago commented Mar 14, 2016

I'm still assuming this could be useful to others. This version changes the following things:

  • ignore_url_params set on collection and passed through pagination functions, default false
  • no more global ignore_url_params setting
  • twig extension in plugin folder

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);
    }
}

@Sogl
Copy link
Contributor

Sogl commented Oct 8, 2016

@gigago Good change. Why not making a PR?

@gigago
Copy link
Contributor Author

gigago commented Oct 11, 2016

Just made a pull request for this code.

@Sogl
Copy link
Contributor

Sogl commented Oct 11, 2016

@gigago Can you give me a link? Can't see any PR in this repo.

@gigago
Copy link
Contributor Author

gigago commented Oct 11, 2016

https://github.com/gigago/grav-plugin-pagination/pull/1

Sorry I’m a beginner in git. Pretty sure I made some mistakes.

On 11 okt. 2016, at 22:53, Artyom Mezin notifications@github.com wrote:

@gigago https://github.com/gigago Can you give me a link? Can't see any PR in this repo.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #13 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AQShiTEVVms88y2i00mir93w0VFEvonKks5qy_dkgaJpZM4HqT0U.

@Sogl
Copy link
Contributor

Sogl commented Oct 11, 2016

You made a PR in your own plugin fork, but need to do this here ;)

@gigago
Copy link
Contributor Author

gigago commented Oct 11, 2016

I suspected something like that. No problem, was a nice excercise, PR coming =)

On 11 okt. 2016, at 23:03, Artyom Mezin notifications@github.com wrote:

You made a PR in your own plugin fork, but need to do this here ;)


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #13 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AQShiZ2g7ZmmycTuUOAZcetVtXw9NFD8ks5qy_mggaJpZM4HqT0U.

@inktrap
Copy link

inktrap commented Oct 12, 2017

@gigago This is such a nice addition … would be a shame to forget the PR ;)
👍

@rhukster
Copy link
Member

Was there ever a pull request created for this?

@gigago
Copy link
Contributor Author

gigago commented Oct 13, 2017

This has been merged earlier this year: #22

@inktrap
Copy link

inktrap commented Oct 13, 2017

So, this can be closed?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants