diff --git a/formwork/Core/Formwork.php b/formwork/Core/Formwork.php index 012d9280..78d31ea3 100755 --- a/formwork/Core/Formwork.php +++ b/formwork/Core/Formwork.php @@ -197,8 +197,6 @@ class Formwork $resource = $this->router->dispatch(); - $resource->template()->loadController(); - if (is_null($this->site->currentPage())) { $this->site->setCurrentPage($resource); } diff --git a/formwork/Core/Template.php b/formwork/Core/Template.php index 6c75e14f..dbf5db5c 100755 --- a/formwork/Core/Template.php +++ b/formwork/Core/Template.php @@ -122,6 +122,15 @@ class Template $this->vars = array_merge($this->vars, $vars); + $isCurrentPage = $this->page->isCurrent(); + + $this->loadController(); + + // Render correct page if the controller has changed the current one + if ($isCurrentPage && !$this->page->isCurrent()) { + return Formwork::instance()->site()->currentPage()->template()->render($vars, $return); + } + ob_start(); $this->rendering = true; @@ -146,23 +155,6 @@ class Template ob_end_flush(); } - /** - * Load template controller if exists - */ - public function loadController() - { - if ($this->rendering) { - throw new RuntimeException(__METHOD__ . ' not allowed while rendering'); - } - - $controllerFile = $this->path . 'controllers' . DS . $this->name . '.php'; - - if (FileSystem::exists($controllerFile)) { - extract($this->vars); - $this->vars = array_merge($this->vars, (array) include $controllerFile); - } - } - /** * Return an array containing the default data * @@ -177,6 +169,23 @@ class Template ); } + /** + * Load template controller if exists + */ + protected function loadController() + { + if ($this->rendering) { + throw new RuntimeException(__METHOD__ . ' not allowed while rendering'); + } + + $controllerFile = $this->path . 'controllers' . DS . $this->name . '.php'; + + if (FileSystem::exists($controllerFile)) { + extract($this->vars); + $this->vars = array_merge($this->vars, (array) include $controllerFile); + } + } + /** * Get Assets instance *