diff --git a/src/View/Cell.php b/src/View/Cell.php index 6279099845d..f1df58e971d 100644 --- a/src/View/Cell.php +++ b/src/View/Cell.php @@ -187,6 +187,8 @@ public function render($template = null) )); } + $builder = $this->viewBuilder(); + if ($template !== null && strpos($template, '/') === false && strpos($template, '.') === false @@ -194,16 +196,16 @@ public function render($template = null) $template = Inflector::underscore($template); } if ($template === null) { - $template = $this->viewBuilder()->template() ?: $this->template; + $template = $builder->template() ?: $this->template; } - - $builder = $this->viewBuilder(); - $builder->layout(false); - $builder->template($template); + $builder->layout(false) + ->template($template); $className = substr(strrchr(get_class($this), "\\"), 1); $name = substr($className, 0, -4); - $builder->templatePath('Cell' . DS . $name); + if (!$builder->templatePath()) { + $builder->templatePath('Cell' . DS . $name); + } $this->View = $this->createView(); try { diff --git a/tests/TestCase/View/CellTest.php b/tests/TestCase/View/CellTest.php index 5642b0cec4f..426fbd0a1b2 100644 --- a/tests/TestCase/View/CellTest.php +++ b/tests/TestCase/View/CellTest.php @@ -142,6 +142,20 @@ public function testSettingCellTemplateFromAction() $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template()); } + /** + * Tests that cell action setting the templatePath + * + * @return void + */ + public function testSettingCellTemplatePathFromAction() + { + $appCell = $this->View->cell('Articles::customTemplatePath'); + + $this->assertContains('Articles subdir custom_template_path template', "{$appCell}"); + $this->assertEquals('custom_template_path', $appCell->template); + $this->assertEquals('Cell/Articles/Subdir', $appCell->viewBuilder()->templatePath()); + } + /** * Tests that cell action setting the template using the ViewBuilder renders the correct template * diff --git a/tests/test_app/TestApp/Template/Cell/Articles/Subdir/custom_template_path.ctp b/tests/test_app/TestApp/Template/Cell/Articles/Subdir/custom_template_path.ctp new file mode 100644 index 00000000000..32f4a4f1336 --- /dev/null +++ b/tests/test_app/TestApp/Template/Cell/Articles/Subdir/custom_template_path.ctp @@ -0,0 +1 @@ +Articles subdir custom_template_path template diff --git a/tests/test_app/TestApp/View/Cell/ArticlesCell.php b/tests/test_app/TestApp/View/Cell/ArticlesCell.php index ae12772fc9c..c6f57b82e91 100644 --- a/tests/test_app/TestApp/View/Cell/ArticlesCell.php +++ b/tests/test_app/TestApp/View/Cell/ArticlesCell.php @@ -82,6 +82,17 @@ public function customTemplateViewBuilder() $this->viewBuilder()->template('alternate_teaser_list'); } + /** + * Renders a template in a custom templatePath + * The template is set using the ViewBuilder bound to the Cell + * + * @return void + */ + public function customTemplatePath() + { + $this->viewBuilder()->templatePath('Cell/Articles/Subdir'); + } + /** * Simple echo. *