LoginSignup
15
14

More than 5 years have passed since last update.

Laravel 5 で Paginator->render() する時、自前のテンプレートを用いる方法

Posted at

Paginator 提供が提供してくれるデフォルトの View は Bootstrap3 に最適化されています。

例えば、ガラケー対応が必要な場合など、render() の出力(というか view? )を変更したいときの方法。

結論: Presenter を使いましょう

例えば、以下の様な Class を用意します。

app/Presenters/CustomPresenter.php
<?php namespace App\Presenters;

use Illuminate\Pagination\BootstrapThreePresenter;

class CustomPresenter extends BootstrapThreePresenter {

    public function render()
    {
        if( ! $this->hasPages())
            return '';

        return sprintf(
            '<div class="pagination">%s %s %s</div>',
            $this->getPreviousButton(),
            $this->getLinks(),
            $this->getNextButton()
        );
    }

    /**
     * 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 '<a href="'.htmlentities($url).'"'.$rel.'>'.$page.'</a>';
    }

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

    /**
     * Get HTML wrapper for active text.
     *
     * @param  string  $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<span>['.$text.']</span>';
    }
}

上記では BootstrapThreePresenter を継承していますが、 PresenterContract を implements すればOKのはず。

あとは好きにメソッドを書き換えたりするだけ。

作った Presenter を使う

view や controller などで、 (new MobilePresenter($paginator))->render(); として使います。

※値を持ち回る場合は $paginator->appends(Input::all())を忘れずに。

15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14