Skip to content

Commit

Permalink
Allow customizing templates for prev() and next().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 7, 2015
1 parent 999b0ef commit 058daad
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/View/Helper/PaginatorHelper.php
Expand Up @@ -244,10 +244,24 @@ protected function _toggledLink($text, $enabled, $options, $templates)
}
$text = $options['escape'] ? h($text) : $text;

$templater = $this->templater();
$newTemplates = !empty($options['templates']) ? $options['templates'] : false;
if ($newTemplates) {
$templater->push();
$templateMethod = is_string($options['templates']) ? 'load' : 'add';
$templater->{$templateMethod}($options['templates']);
}

if (!$enabled) {
return $this->templater()->format($template, [
$out = $templater->format($template, [
'text' => $text,
]);

if ($newTemplates) {
$templater->pop();
}

return $out;
}
$paging = $this->params($options['model']);

Expand All @@ -256,10 +270,17 @@ protected function _toggledLink($text, $enabled, $options, $templates)
['page' => $paging['page'] + $options['step']]
);
$url = $this->generateUrl($url, $options['model']);
return $this->templater()->format($template, [

$out = $templater->format($template, [
'url' => $url,
'text' => $text,
]);

if ($newTemplates) {
$templater->pop();
}

return $out;
}

/**
Expand All @@ -273,6 +294,9 @@ protected function _toggledLink($text, $enabled, $options, $templates)
* - `escape` Whether you want the contents html entity encoded, defaults to true
* - `model` The model to use, defaults to PaginatorHelper::defaultModel()
* - `url` An array of additional URL options to use for link generation.
* - `templates` An array of templates, or template file name containing the
* templates you'd like to use when generating the link for previous page.
* The helper's original templates will be restored once prev() is done.
*
* @param string $title Title for the link. Defaults to '<< Previous'.
* @param array $options Options for pagination link. See above for list of keys.
Expand Down Expand Up @@ -309,6 +333,9 @@ public function prev($title = '<< Previous', array $options = [])
* - `escape` Whether you want the contents html entity encoded, defaults to true
* - `model` The model to use, defaults to PaginatorHelper::defaultModel()
* - `url` An array of additional URL options to use for link generation.
* - `templates` An array of templates, or template file name containing the
* templates you'd like to use when generating the link for next page.
* The helper's original templates will be restored once next() is done.
*
* @param string $title Title for the link. Defaults to 'Next >>'.
* @param array $options Options for pagination link. See above for list of keys.
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -915,6 +915,18 @@ public function testPrev()
'/li'
];
$this->assertHtml($expected, $result);

$result = $this->Paginator->prev('Prev', [
'templates' => [
'prevActive' => '<a rel="prev" href="{{url}}">{{text}}</a>'
]
]);
$expected = [
'a' => ['href' => '/index', 'rel' => 'prev'],
'Prev',
'/a',
];
$this->assertHtml($expected, $result);
}

/**
Expand Down Expand Up @@ -960,6 +972,18 @@ public function testNext()
];
$this->assertHtml($expected, $result);

$result = $this->Paginator->next('Next', [
'templates' => [
'nextActive' => '<a rel="next" href="{{url}}">{{text}}</a>'
]
]);
$expected = [
'a' => ['href' => '/index?page=2', 'rel' => 'next'],
'Next',
'/a',
];
$this->assertHtml($expected, $result);

$result = $this->Paginator->next('Next >>', ['escape' => false]);
$expected = [
'li' => ['class' => 'next'],
Expand Down

0 comments on commit 058daad

Please sign in to comment.