Browse Source

It should be possible to set the template from the action of the cell

Yves P 10 years ago
parent
commit
225e8dd4b6

+ 11 - 11
src/View/Cell.php

@@ -176,6 +176,17 @@ abstract class Cell
         }
 
         $render = function () use ($template) {
+            try {
+                $reflect = new ReflectionMethod($this, $this->action);
+                $reflect->invokeArgs($this, $this->args);
+            } catch (ReflectionException $e) {
+                throw new BadMethodCallException(sprintf(
+                    'Class %s does not have a "%s" method.',
+                    get_class($this),
+                    $this->action
+                ));
+            }
+
             if ($template !== null &&
                 strpos($template, '/') === false &&
                 strpos($template, '.') === false
@@ -194,17 +205,6 @@ abstract class Cell
             $name = substr($className, 0, -4);
             $builder->templatePath('Cell' . DS . $name);
 
-            try {
-                $reflect = new ReflectionMethod($this, $this->action);
-                $reflect->invokeArgs($this, $this->args);
-            } catch (ReflectionException $e) {
-                throw new BadMethodCallException(sprintf(
-                    'Class %s does not have a "%s" method.',
-                    get_class($this),
-                    $this->action
-                ));
-            }
-
             $this->View = $this->createView();
             try {
                 return $this->View->render($template);

+ 42 - 0
tests/TestCase/View/CellTest.php

@@ -133,6 +133,24 @@ class CellTest extends TestCase
     }
 
     /**
+     * Tests that cell action setting the template renders the correct template
+     *
+     * @return void
+     */
+    public function testSettingCellTemplateFromAction()
+    {
+        $appCell = $this->View->cell('Articles::customTemplate');
+
+        $this->assertContains('This is the alternate template', "{$appCell}");
+        $this->assertEquals('alternate_teaser_list', $appCell->template);
+
+        $appCell = $this->View->cell('Articles::customTemplate');
+
+        $this->assertContains('This is the alternate template', $appCell->render());
+        $this->assertEquals('alternate_teaser_list', $appCell->template);
+    }
+
+    /**
      * Tests manual render() invocation.
      *
      * @return void
@@ -361,4 +379,28 @@ class CellTest extends TestCase
         $this->assertEquals("dummy\n", $result);
         Cache::drop('cell');
     }
+
+    /**
+     * Test cached render.
+     *
+     * @return void
+     */
+    public function testCachedRenderSimpleCustomTemplate()
+    {
+        $mock = $this->getMock('Cake\Cache\CacheEngine');
+        $mock->method('init')
+            ->will($this->returnValue(true));
+        $mock->method('read')
+            ->will($this->returnValue(false));
+        $mock->expects($this->once())
+            ->method('write')
+            ->with('cell_test_app_view_cell_articles_cell_customTemplate', "<h1>This is the alternate template</h1>\n");
+        Cache::config('default', $mock);
+
+        $cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
+        $result = $cell->render();
+        $this->assertContains('This is the alternate template', $result);
+
+        Cache::drop('default');
+    }
 }

+ 1 - 0
tests/test_app/TestApp/Template/Cell/Articles/alternate_teaser_list.ctp

@@ -0,0 +1 @@
+<h1>This is the alternate template</h1>

+ 16 - 0
tests/test_app/TestApp/View/Cell/ArticlesCell.php

@@ -52,6 +52,22 @@ class ArticlesCell extends \Cake\View\Cell
     }
 
     /**
+     * Renders articles in teaser view mode.
+     *
+     * @return void
+     */
+    public function customTemplate()
+    {
+        $this->template = 'alternate_teaser_list';
+        $this->set('articles', [
+            ['title' => 'Lorem ipsum', 'body' => 'dolorem sit amet'],
+            ['title' => 'Usectetur adipiscing eli', 'body' => 'tortor, in tincidunt sem dictum vel'],
+            ['title' => 'Topis semper blandit eu non', 'body' => 'alvinar diam convallis non. Nullam pu'],
+            ['title' => 'Suspendisse gravida neque', 'body' => 'pellentesque sed scelerisque libero'],
+        ]);
+    }
+
+    /**
      * Simple echo.
      *
      * @param string $msg1