Skip to content

Commit a578ec5

Browse files
committed
Move first() to use StringTemplate.
Add templates for first() and last(). Migrate first() over.
1 parent 8819b85 commit a578ec5

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,13 +1810,13 @@ public function testFirstFullBaseUrl() {
18101810

18111811
$result = $this->Paginator->first();
18121812
$expected = array(
1813-
'<span',
1813+
'li' => ['class' => 'first'],
18141814
array('a' => array(
18151815
'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC', 'rel' => 'first'
18161816
)),
18171817
'&lt;&lt; first',
18181818
'/a',
1819-
'/span',
1819+
'/li',
18201820
);
18211821
$this->assertTags($result, $expected);
18221822
}
@@ -1830,23 +1830,23 @@ public function testFirstBoundaries() {
18301830
$this->Paginator->request->params['paging']['Article']['page'] = 3;
18311831
$result = $this->Paginator->first();
18321832
$expected = array(
1833-
'<span',
1833+
'li' => ['class' => 'first'],
18341834
'a' => array('href' => '/index', 'rel' => 'first'),
18351835
'&lt;&lt; first',
18361836
'/a',
1837-
'/span'
1837+
'/li'
18381838
);
18391839
$this->assertTags($result, $expected);
18401840

18411841
$result = $this->Paginator->first(2);
18421842
$expected = array(
1843-
'<span',
1843+
'<li',
18441844
array('a' => array('href' => '/index')), '1', '/a',
1845-
'/span',
1845+
'/li',
18461846
' | ',
1847-
'<span',
1847+
'<li',
18481848
array('a' => array('href' => '/index?page=2')), '2', '/a',
1849-
'/span'
1849+
'/li'
18501850
);
18511851
$this->assertTags($result, $expected);
18521852

Cake/View/Helper/PaginatorHelper.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class PaginatorHelper extends Helper {
7878
'prevDisabled' => '<li class="prev disabled"><span>{{text}}</span></li>',
7979
'counterRange' => '{{start}} - {{end}} of {{count}}',
8080
'counterPages' => '{{page}} of {{pages}}',
81+
'first' => '<li class="first"><a rel="first" href="{{url}}">{{text}}</a></li>',
82+
'last' => '<li class="last"><a rel="last" href="{{url}}">{{text}}</a></li>',
83+
'number' => '<li><a href="{{url}}">{{text}}</a></li>',
84+
'ellipsis' => '...',
85+
'separator' => ' | ',
8186
];
8287

8388
/**
@@ -276,9 +281,6 @@ protected function _toggledLink($text, $enabled, $options, $templates) {
276281
]);
277282
}
278283

279-
if (!empty($this->options['url'])) {
280-
$options['url'] = array_merge($this->options['url'], $options['url']);
281-
}
282284
$url = array_merge(
283285
$options['url'],
284286
['page' => $paging['page'] + $options['step']]
@@ -469,6 +471,10 @@ public function url($options = array(), $asArray = false, $model = null) {
469471
'sort' => $paging['sort'],
470472
'direction' => $paging['direction'],
471473
];
474+
475+
if (!empty($this->options['url'])) {
476+
$url = array_merge($this->options['url'], $url);
477+
}
472478
$url = array_merge(array_filter($url), $options);
473479

474480
if (!empty($url['page']) && $url['page'] == 1) {
@@ -768,55 +774,51 @@ public function numbers($options = array()) {
768774
*
769775
* ### Options:
770776
*
771-
* - `tag` The tag wrapping tag you want to use, defaults to 'span'
772-
* - `after` Content to insert after the link/tag
773777
* - `model` The model to use defaults to PaginatorHelper::defaultModel()
774-
* - `separator` Content between the generated links, defaults to ' | '
775-
* - `ellipsis` Content for ellipsis, defaults to '...'
778+
* - `escape` Whether or not to HTML escape the text.
776779
*
777780
* @param string|integer $first if string use as label for the link. If numeric, the number of page links
778781
* you want at the beginning of the range.
779782
* @param array $options An array of options.
780783
* @return string numbers string.
781784
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
782785
*/
783-
public function first($first = '<< first', $options = array()) {
786+
public function first($first = '<< first', $options = []) {
784787
$options = array_merge(
785-
array(
786-
'tag' => 'span',
787-
'after' => null,
788-
'model' => $this->defaultModel(),
789-
'separator' => ' | ',
790-
'ellipsis' => '...',
791-
'class' => null
792-
),
793-
(array)$options);
788+
['model' => $this->defaultModel(), 'escape' => true],
789+
(array)$options
790+
);
794791

795-
$params = array_merge(array('page' => 1), (array)$this->params($options['model']));
792+
$params = array_merge(
793+
['page' => 1],
794+
(array)$this->params($options['model'])
795+
);
796796
unset($options['model']);
797797

798798
if ($params['pageCount'] <= 1) {
799799
return false;
800800
}
801-
extract($options);
802-
unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
803801

804802
$out = '';
805803

806804
if (is_int($first) && $params['page'] >= $first) {
807-
if ($after === null) {
808-
$after = $ellipsis;
809-
}
805+
$ellipsis = $this->_templater->format('ellipsis', []);
806+
$separator = $this->_templater->format('separator', []);
810807
for ($i = 1; $i <= $first; $i++) {
811-
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
808+
$out .= $this->_templater->format('number', [
809+
'url' => $this->url(['page' => $i]),
810+
'text' => $i
811+
]);
812812
if ($i != $first) {
813813
$out .= $separator;
814814
}
815815
}
816-
$out .= $after;
817816
} elseif ($params['page'] > 1 && is_string($first)) {
818-
$options += array('rel' => 'first');
819-
$out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
817+
$first = $options['escape'] ? h($first) : $first;
818+
$out .= $this->_templater->format('first', [
819+
'url' => $this->url(['page' => 1]),
820+
'text' => $first
821+
]);
820822
}
821823
return $out;
822824
}

0 commit comments

Comments
 (0)