diff --git a/code/controllers/ContentController.php b/code/controllers/ContentController.php old mode 100644 new mode 100755 index 113749bc..2bab3160 --- a/code/controllers/ContentController.php +++ b/code/controllers/ContentController.php @@ -369,8 +369,39 @@ HTML; } return i18n::convert_rfc1766($locale); + } + + + /** + * Return an SSViewer object to render the template for the current page. + * + * @param $action string + * + * @return SSViewer + */ + public function getViewer($action) { + // Manually set templates should be dealt with by Controller::getViewer() + if(isset($this->templates[$action]) && $this->templates[$action] + || (isset($this->templates['index']) && $this->templates['index']) + || $this->template + ) { + return parent::getViewer($action); + } + + // Prepare action for template search + if($action == "index") $action = ""; + else $action = '_' . $action; + + // Find templates by dataRecord + $templates = SSViewer::get_templates_by_class(get_class($this->dataRecord), $action, "SiteTree"); + + // Next, we need to add templates for all controllers + $templates += SSViewer::get_templates_by_class(get_class($this), $action, "Controller"); + + return new SSViewer($templates); } + /** * This action is called by the installation system */ diff --git a/tests/controller/ContentControllerTest.php b/tests/controller/ContentControllerTest.php old mode 100644 new mode 100755 index 4c8e0377..5c062de3 --- a/tests/controller/ContentControllerTest.php +++ b/tests/controller/ContentControllerTest.php @@ -121,6 +121,44 @@ class ContentControllerTest extends FunctionalTest { $this->get($page->RelativeLink())->getBody(), '"sitetree_link" shortcodes get parsed properly' ); + } + + + /** + * Tests that {@link ContentController::getViewer()} chooses the correct templates. + * + * @covers ContentController::getViewer() + **/ + public function testGetViewer() { + + // // Test a page without a controller (ContentControllerTest_PageWithoutController.ss) + $page = new ContentControllerTestPageWithoutController(); + $page->URLSegment = "test"; + $page->write(); + $page->publish("Stage", "Live"); + + $response = $this->get($page->RelativeLink()); + $this->assertEquals("ContentControllerTestPageWithoutController", $response->getBody()); + + + // // This should fall over to user Page.ss + $page = new ContentControllerTestPage(); + $page->URLSegment = "test"; + $page->write(); + $page->publish("Stage", "Live"); + + $response = $this->get($page->RelativeLink()); + $this->assertEquals("Foo", $response->getBody()); + + + // Test that the action template is rendered. + $page = new ContentControllerTestPage(); + $page->URLSegment = "page-without-controller"; + $page->write(); + $page->publish("Stage", "Live"); + + $response = $this->get($page->RelativeLink("test")); + $this->assertEquals("ContentControllerTestPage_test", $response->getBody()); } } @@ -142,3 +180,13 @@ class ContentControllerTest_Page_Controller extends Page_Controller { } } + +// For testing templates +class ContentControllerTestPageWithoutController extends Page { } + +class ContentControllerTestPage extends Page { } +class ContentControllerTestPage_Controller extends Page_Controller { + private static $allowed_actions = array( + "test", + ); +} diff --git a/tests/templates/ContentControllerTestPageWithoutController.ss b/tests/templates/ContentControllerTestPageWithoutController.ss new file mode 100755 index 00000000..91df8989 --- /dev/null +++ b/tests/templates/ContentControllerTestPageWithoutController.ss @@ -0,0 +1 @@ +ContentControllerTestPageWithoutController \ No newline at end of file diff --git a/tests/templates/ContentControllerTestPage_test.ss b/tests/templates/ContentControllerTestPage_test.ss new file mode 100755 index 00000000..31438862 --- /dev/null +++ b/tests/templates/ContentControllerTestPage_test.ss @@ -0,0 +1 @@ +ContentControllerTestPage_test \ No newline at end of file