2008-02-21 04:33:03 +00:00
{{Doctrine_Pager_Layout}} does a really good job, but sometimes it is not enough. Let's suppose a situation where you have to create a layout of pagination like this one:
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
<< < 1 2 3 4 5 > >>
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
Currently, it is impossible with raw {{Doctrine_Pager_Layout}}. But if you extend it and use the available methods, you can achieve it. The base Layout class provides you some methods that can be used to create your own implementation. They are:
2007-12-22 19:00:58 +00:00
<code type="php">
2008-02-21 04:33:03 +00:00
// $this refers to an instance of Doctrine_Pager_Layout
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
// Defines a mask replacement. When parsing template, it converts replacement
// masks into new ones (or values), allowing to change masks behavior on the fly
$this->addMaskReplacement($oldMask, $newMask, $asValue = false);
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
// Remove a mask replacement
$this->removeMaskReplacement($oldMask);
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
// Remove all mask replacements
$this->cleanMaskReplacements();
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
// Parses the template and returns the string of a processed page
$this->processPage($options = array()); // Needs at least page_number offset in $options array
</code>
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
Now that you have a small tip of useful methods to be used when extending {{Doctrine_Pager_Layout}}, it's time to see our implemented class:
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
<code type="php">
class PagerLayoutWithArrows extends Doctrine_Pager_Layout
{
public function display($options = array(), $return = false)
{
$pager = $this->getPager();
$str = '';
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
// First page
$this->addMaskReplacement('page', '«', true);
$options['page_number'] = $pager->getFirstPage();
$str .= $this->processPage($options);
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
// Previous page
$this->addMaskReplacement('page', '‹', true);
$options['page_number'] = $pager->getPreviousPage();
$str .= $this->processPage($options);
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
// Pages listing
$this->removeMaskReplacement('page');
2007-12-22 19:00:58 +00:00
$str .= parent::display($options, true);
// Next page
2008-02-21 04:33:03 +00:00
$this->addMaskReplacement('page', '›', true);
$options['page_number'] = $pager->getNextPage();
$str .= $this->processPage($options);
2007-12-22 19:00:58 +00:00
// Last page
2008-02-21 04:33:03 +00:00
$this->addMaskReplacement('page', '»', true);
$options['page_number'] = $pager->getLastPage();
$str .= $this->processPage($options);
2007-12-22 19:00:58 +00:00
// Possible wish to return value instead of print it on screen
2008-02-21 04:33:03 +00:00
if ($return) {
return $str;
}
2007-12-22 19:00:58 +00:00
2008-02-21 04:33:03 +00:00
echo $str;
2007-12-22 19:00:58 +00:00
}
}
</code>