Decorators

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 3 decorators. Generic, Bootstrap (v3), and Foundation (v6).

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.

1
2
3
4
5
6
7
<?php
$config = [
    'foo'=>'bar',
    'lorem'=>'ipsum'
];

$decorator = new Generic($config);

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?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()

1
2
3
4
5
6
7
8
9
<?php

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

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

...

Default configuration

The default configuration looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?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

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