Browse Source

Add templates option to numbers()

Much like the templates options in FormHelper, this option lets you set
the templates for one numbers() call. Or have numbers() use different
templates than the rest of your pagination buttons.

Refs #4171
mark_story 11 years ago
parent
commit
6888361906

+ 17 - 8
src/View/Helper/PaginatorHelper.php

@@ -596,13 +596,19 @@ class PaginatorHelper extends Helper {
 		$options += $defaults;
 
 		$params = (array)$this->params($options['model']) + array('page' => 1);
-
 		if ($params['pageCount'] <= 1) {
 			return false;
 		}
 
+		$templater = $this->templater();
+		if (isset($options['templates'])) {
+			$templater->push();
+			$method = is_string($options['templates']) ? 'load' : 'add';
+			$templater->{$method}($options['templates']);
+		}
+
 		$out = '';
-		$ellipsis = $this->templater()->format('ellipsis', []);
+		$ellipsis = $templater->format('ellipsis', []);
 
 		if ($options['modulus'] && $params['pageCount'] > $options['modulus']) {
 			$half = intval($options['modulus'] / 2);
@@ -632,10 +638,10 @@ class PaginatorHelper extends Helper {
 					'text' => $i,
 					'url' => $this->generateUrl(['page' => $i], $options['model']),
 				];
-				$out .= $this->templater()->format('number', $vars);
+				$out .= $templater->format('number', $vars);
 			}
 
-			$out .= $this->templater()->format('current', [
+			$out .= $templater->format('current', [
 				'text' => $params['page'],
 				'url' => $this->generateUrl(['page' => $params['page']], $options['model']),
 			]);
@@ -646,7 +652,7 @@ class PaginatorHelper extends Helper {
 					'text' => $i,
 					'url' => $this->generateUrl(['page' => $i], $options['model']),
 				];
-				$out .= $this->templater()->format('number', $vars);
+				$out .= $templater->format('number', $vars);
 			}
 
 			if ($end != $params['page']) {
@@ -654,7 +660,7 @@ class PaginatorHelper extends Helper {
 					'text' => $i,
 					'url' => $this->generateUrl(['page' => $end], $options['model']),
 				];
-				$out .= $this->templater()->format('number', $vars);
+				$out .= $templater->format('number', $vars);
 			}
 
 			$out .= $options['after'];
@@ -672,7 +678,7 @@ class PaginatorHelper extends Helper {
 
 			for ($i = 1; $i <= $params['pageCount']; $i++) {
 				if ($i == $params['page']) {
-					$out .= $this->templater()->format('current', [
+					$out .= $templater->format('current', [
 						'text' => $params['page'],
 						'url' => $this->generateUrl(['page' => $params['page']], $options['model']),
 					]);
@@ -681,12 +687,15 @@ class PaginatorHelper extends Helper {
 						'text' => $i,
 						'url' => $this->generateUrl(['page' => $i], $options['model']),
 					];
-					$out .= $this->templater()->format('number', $vars);
+					$out .= $templater->format('number', $vars);
 				}
 			}
 
 			$out .= $options['after'];
 		}
+		if (isset($options['templates'])) {
+			$templater->pop();
+		}
 
 		return $out;
 	}

+ 39 - 0
tests/TestCase/View/Helper/PaginatorHelperTest.php

@@ -1254,6 +1254,45 @@ class PaginatorHelperTest extends TestCase {
 	}
 
 /**
+ * Test that numbers() lets you overwrite templates.
+ *
+ * The templates file has no li elements.
+ *
+ * @return void
+ */
+	public function testNumbersTemplates() {
+		$this->Paginator->request->params['paging'] = [
+			'Client' => [
+				'page' => 8,
+				'current' => 3,
+				'count' => 30,
+				'prevPage' => false,
+				'nextPage' => 2,
+				'pageCount' => 15,
+			]
+		];
+		$result = $this->Paginator->numbers(['templates' => 'htmlhelper_tags']);
+		$expected = [
+			['a' => ['href' => '/index?page=4']], '4', '/a',
+			['a' => ['href' => '/index?page=5']], '5', '/a',
+			['a' => ['href' => '/index?page=6']], '6', '/a',
+			['a' => ['href' => '/index?page=7']], '7', '/a',
+			'span' => ['class' => 'active'], '8', '/span',
+			['a' => ['href' => '/index?page=9']], '9', '/a',
+			['a' => ['href' => '/index?page=10']], '10', '/a',
+			['a' => ['href' => '/index?page=11']], '11', '/a',
+			['a' => ['href' => '/index?page=12']], '12', '/a',
+		];
+		$this->assertTags($result, $expected);
+
+		$this->assertContains(
+			'<li',
+			$this->Paginator->templater()->get('current'),
+			'Templates were not restored.'
+		);
+	}
+
+/**
  * Test modulus option for numbers()
  *
  * @return void

+ 3 - 1
tests/test_app/config/htmlhelper_tags.php

@@ -4,5 +4,7 @@ $config = [
 	'formstart' => 'start form',
 	'formend' => 'finish form',
 	'hiddenblock' => '<div class="hidden">{{content}}</div>',
-	'inputContainer' => '{{content}}'
+	'inputContainer' => '{{content}}',
+	'number' => '<a href="{{url}}">{{text}}</a>',
+	'current' => '<span class="active">{{text}}</span>',
 ];