schir1964's avatar

Foundation 6 Presenter for Paginator

Illuminate\Pagination class works really well but only has Presenters for Bootstrap. I decided to take a crack at making a Presenter for Foundation 6 and amazingly it only took about an hour of actual code changes (a few more for reading and analyzing the code).

Anyway, I copied the following two files to my App directory and renamed them and made the necessary changes for implementation and Foundation 6 styles.

  • laravel/framework/src/Illuminate/Pagination/BootstrapThreePresenter.php
  • laravel/framework/src/Illuminate/Pagination/BootstrapThreeNextPreviousButtonRendererTrait.php

Just save the following two files into your App directory and then in your Blade template you simply pass in the Presenter to your Pagination object (usually instantiated in the controller and passed into the template).

Blade: Passing Presenter Into Paginator Object

{!! $paginatorObj->links(new \App\Foundation6Presenter($paginatorObj)) !!}

Foundation6Presenter.php

<?php

namespace App;

use Illuminate\Support\HtmlString;
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract;
use Illuminate\Contracts\Pagination\Presenter as PresenterContract;
use Illuminate\Pagination\UrlWindowPresenterTrait;
use Illuminate\Pagination\UrlWindow;

class Foundation6Presenter implements PresenterContract
{
    use Foundation6NextPreviousButtonRendererTrait, UrlWindowPresenterTrait;

    /**
     * The paginator implementation.
     *
     * @var \Illuminate\Contracts\Pagination\Paginator
     */
    protected $paginator;

    /**
     * The URL window data structure.
     *
     * @var array
     */
    protected $window;

    /**
     * Create a new Bootstrap presenter instance.
     *
     * @param  \Illuminate\Contracts\Pagination\Paginator  $paginator
     * @param  \Illuminate\Pagination\UrlWindow|null  $window
     * @return void
     */
    public function __construct(PaginatorContract $paginator, UrlWindow $window = null)
    {
        $this->paginator = $paginator;
        $this->window = is_null($window) ? UrlWindow::make($paginator) : $window->get();
    }

    /**
     * Determine if the underlying paginator being presented has pages to show.
     *
     * @return bool
     */
    public function hasPages()
    {
        return $this->paginator->hasPages();
    }

    /**
     * Convert the URL window into Bootstrap HTML.
     *
     * @return \Illuminate\Support\HtmlString
     */
    public function render()
    {
        if ($this->hasPages()) {
            return new HtmlString(sprintf(
                '<ul class="pagination text-center" role="navigation" aria-label="Pagination">%s %s %s</ul>',
                $this->getPreviousButton(),
                $this->getLinks(),
                $this->getNextButton()
            ));
        }

        return '';
    }

    /**
     * Get HTML wrapper for an available page link.
     *
     * @param  string  $url
     * @param  int  $page
     * @param  string|null  $rel
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page, $rel = null)
    {
        $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';

        return '<li><a href="'.htmlentities($url).'"'.$rel.' aria-label="Page '.$page.'">'.$page.'</a></li>';
    }

    /**
     * Get HTML wrapper for disabled text.
     *
     * @param  string  $text
     * @return string
     */
    protected function getDisabledTextWrapper($text)
    {
        return '<li class="disabled">'.$text.'</li>';
    }

    /**
     * Get HTML wrapper for active text.
     *
     * @param  string  $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<li class="current"><span class="show-for-sr">You&#39;re on page </span>'.$text.'</li>';
    }

    /**
     * Get a pagination "dot" element.
     *
     * @return string
     */
    protected function getDots()
    {
        return '<li class="ellipsis"></li>';
    }

    /**
     * Get the current page from the paginator.
     *
     * @return int
     */
    protected function currentPage()
    {
        return $this->paginator->currentPage();
    }

    /**
     * Get the last page from the paginator.
     *
     * @return int
     */
    protected function lastPage()
    {
        return $this->paginator->lastPage();
    }
}

Foundation6NextPreviousButtonRendererTrait.php

<?php

namespace App;

trait Foundation6NextPreviousButtonRendererTrait
{
    /**
     * Get the previous page pagination element.
     *
     * @param  string  $text
     * @return string
     */
    public function getPreviousButton($text = '«')
    {
        // If the current page is less than or equal to one, it means we can't go any
        // further back in the pages, so we will render a disabled previous button
        // when that is the case. Otherwise, we will give it an active "status".
        if ($this->paginator->currentPage() <= 1) {
            return '<li class="pagination-previous disabled">Previous</li>';
        }

        $url = $this->paginator->url($this->paginator->currentPage() - 1);

        return '<li class="pagination-previous"><a href="'.htmlentities($url).'" aria-label="Previous page">Previous</a></li>';
    }

    /**
     * Get the next page pagination element.
     *
     * @param  string  $text
     * @return string
     */
    public function getNextButton($text = '»')
    {
        // If the current page is greater than or equal to the last page, it means we
        // can't go any further into the pages, as we're already on this last page
        // that is available, so we will make it the "next" link style disabled.
        if (! $this->paginator->hasMorePages()) {
            return '<li class="pagination-next disabled">Next</li>';
        }

        $url = $this->paginator->url($this->paginator->currentPage() + 1);

        return '<li class="pagination-next"><a href="'.htmlentities($url).'" aria-label="Next page">Next</a></li>';
    }
}
0 likes
0 replies

Please or to participate in this conversation.