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

CakePHP3のPaginator独自タグの設定が楽になった!

More than 5 years have passed since last update.

CakePHP3のPaginator独自タグの設定が楽になった!

CakePHPにはデフォルトでPaginatorの機能がついていますが、そこで出力されるタグがデザインのページャと合わないことはよくあると思います。

CakePHP2では・・・

<div class="paging">
    <?php echo $this->Paginator->prev('< 前', array('tag' => 'span'), null, array('class' => 'disabled')); ?>
    <?php echo $this->Paginator->numbers(array('tag' => 'span','before' => ' ','after' => ' ','separator' => '  ','modulus' => 4)); ?>
    <?php echo $this->Paginator->next('次 >', array('tag' => 'span'), null, array('class' => 'disabled')); ?>
</div>

こんな感じで引数に色々設定することである程度設定可能でした。

でも、限界があってデザインに合わせるために結局PaginatorHelperを継承した独自のヘルパーを作成する羽目になることなってよくありましたよね・・・

CakePHP3では!

こんな感じで設定可能です。

/config/paginator-templates.php

<?php
return [
    'nextActive' => '<span class="next"><a rel="next" href="{{url}}">{{text}}</a></span>',
    'nextDisabled' => '<span class="disabled">{{text}}</span>',
    'prevActive' => '<span class="prev"><a rel="prev" href="{{url}}">{{text}}</a></span> | ',
    'prevDisabled' => '<span class="disabled">{{text}}</span> | ',
    'number' => '<span><a href="{{url}}">{{text}}</a></span> | ',
    'current' => '<span class="current">{{text}}</span> | ',
];

Controller

    public $helpers = [
        'Paginator' => ['templates' => 'paginator-templates']
    ];

View

<div class="paging">
    <?= $this->Paginator->prev('<< ' . __('prev')); ?>
    <?= $this->Paginator->numbers(); ?>
    <?= $this->Paginator->next(__('next') . ' >>'); ?>
</div>

参考

デフォルトの設定がコアのPaginatorヘルパーに設定されていますので、それを参考に独自でタグを設定するといいでしょう。

これでデザイナーさんからCakeにあわないようなデザインのページャが来ても安心!

satthi
fusic
個性をかき集めて、驚きの角度から世の中をアップデートしつづける。
https://fusic.co.jp/
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
No 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
ユーザーは見つかりませんでした