Load templates on each template rendering, not only for the current page

This commit is contained in:
Giuseppe Criscione 2019-05-03 18:44:14 +02:00
parent 21573b04d2
commit 045ae83aaa
2 changed files with 26 additions and 19 deletions

View File

@ -197,8 +197,6 @@ class Formwork
$resource = $this->router->dispatch();
$resource->template()->loadController();
if (is_null($this->site->currentPage())) {
$this->site->setCurrentPage($resource);
}

View File

@ -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
*