Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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())を忘れずに。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away