Decorators Overview

Decorators are responsible for styling the resulting data from the pagination. The paginator object is only responsible for getting an array of pages based on the current page, and the total number of records. After that, the decorators do the heavy lifing. They take the page list and create HTML based on that information.

Vespula Paginator comes with 2 decorators. Generic, Bootstrap (v4).

Configuration

Each decorator can have its own decorator-specific configuration. However, there is a base set of configuration options that are important to all decorators.

Configuration options can be specified by passing an array of options to the decorator constructor.

<?php
$config = [
    'foo'=>'bar',
    'lorem'=>'ipsum'
];

$decorator = new Generic($config);

Alternatively, you can set the configuration array later using the setConfig() method.

<?php

$config = [
    'foo'=>'bar',
    'lorem'=>'ipsum'
];

$decorator = new Generic();
$paginator = new Paginator($decorator);

$decorator = $paginator->getDecorator();
$decorator->setConfig($config);

And finally, you can set individual config options by name using setConfigKey().

<?php

$decorator = new Generic();
$paginator = new Paginator($decorator);

$decorator = $paginator->getDecorator();
$decorator->setConfigKey('foo', 'bar');

Default configuration

The default configuration looks like this:

<?php

$config = [
    'container_tag'=>'ul',
    'container_class'=>'pagination',
    'item_class'=>null,
    'item_tag'=>'li',
    'disabled_class'=>'disabled',
    'active_class'=>'active',
    'text_first'=>'First',
    'text_last'=>'Last',
    'text_next'=>'Next',
    'text_previous'=>'Previous',
    'aria_previous'=>'Previous page',
    'aria_next'=>'Next page',
    'aria_last'=>'Last page',
    'aria_first'=>'First page',
    'aria_current'=>'You are on page %s',
    'aria_page'=>'Page %s'
];

Each of these should be self-explanatory. Each of these can be modified as needed for any of the decorators. For example, you can pass translated text in place of English.

Using custom decorators

If you want to use your own decorator, you just need to implement the Vespula\Paginator\Decorator\DecoratorInterface and implement the required methods. The best way to accomplish this is to extend the Vespula\Paginator\Decorator\AbstractDecorator class, as it implements the interface, but also has many useful methods.

The decorators build() method relies on the Vespula\Paginator\Decorator\Element class for creating the html elements.

Example of the Element object

<?php

use Vespula\Paginator\Decorator\Element;

$element = new Element('div');
$element->addAttribute('class', 'foo');
$element->addAttribute('id', 'bar');
$element->setText('Lorem Ipsum');
$element->appendAttribute('class', 'baz');

echo $element;

Outputs

<div class="foo baz" id="bar">Lorem Ipsum</div>