Browse Source

Allow customizing templates for prev() and next().

ADmad 10 years ago
parent
commit
058daad01c

+ 29 - 2
src/View/Helper/PaginatorHelper.php

@@ -244,10 +244,24 @@ class PaginatorHelper extends Helper
         }
         $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']);
 
@@ -256,10 +270,17 @@ class PaginatorHelper extends Helper
             ['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;
     }
 
     /**
@@ -273,6 +294,9 @@ class PaginatorHelper extends Helper
      * - `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.
@@ -309,6 +333,9 @@ class PaginatorHelper extends Helper
      * - `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.

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

@@ -915,6 +915,18 @@ class PaginatorHelperTest extends TestCase
             '/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);
     }
 
     /**
@@ -960,6 +972,18 @@ class PaginatorHelperTest extends TestCase
         ];
         $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'],