Skip to content
Snippets Groups Projects
Unverified Commit 44a06823 authored by Thomas Flori's avatar Thomas Flori
Browse files

pass layout to view and iterate up to parent view

This way we save some duplication of code as a layout is almost the same
as a parent view.
parent 688a08c1
No related branches found
No related tags found
No related merge requests found
......@@ -98,10 +98,11 @@ class Factory
*
* @param string $name
* @param array $data
* @param string|View $layout
* @return View
* @throws \Exception|\LogicException
* @throws \Exception
*/
public function view(string $name, array $data = []): View
public function view(string $name, array $data = [], $layout = null): View
{
$viewLocator = $this->viewLocator;
......@@ -118,7 +119,11 @@ class Factory
throw new \Exception('View ' . $name . ' not found');
}
return new View($this, $viewLocator->getPath($name), $data);
if (is_string($layout)) {
$layout = $this->view('layout::' . $layout);
}
return new View($this, $viewLocator->getPath($name), $data, $layout);
}
/**
......@@ -129,20 +134,13 @@ class Factory
*
* @param string $name
* @param array $data
* @param string|null $layout
* @param string $layout
* @return string
*/
public function render(string $name, array $data = [], string $layout = null): string
{
$view = $this->view($name, $data);
$view = $this->view($name, $data, $layout);
$content = $view->render();
if ($layout && isset($this->namedLocators['layout'])) {
$layout = $this->view('layout::' . $layout);
$layout->setSections(array_merge($view->getSections(), ['content' => $content]));
$content = $layout->render();
}
return $content;
}
......
......@@ -13,11 +13,14 @@ class View
/** @var string */
protected $path;
/** @var array */
protected $sections = [];
/** @var View */
protected $layout;
/** @var View */
protected $parentTemplate;
protected $parentView;
/** @var array */
protected $sections = [];
/** @var string */
protected $sectionName;
......@@ -25,10 +28,11 @@ class View
/** @var bool */
protected $appendSection = false;
public function __construct(Factory $factory, string $path, array $data = [])
public function __construct(Factory $factory, string $path, array $data = [], ?View $layout = null)
{
$this->factory = $factory;
$this->path = $path;
$this->layout = $layout;
$this->data = array_merge($factory->getSharedData(), $data);
}
......@@ -118,9 +122,10 @@ class View
include $this->path;
$content = trim(ob_get_clean());
if ($this->parentTemplate) {
$this->parentTemplate->setSections(array_merge($this->sections, ['content' => $content]));
$content = $this->parentTemplate->render();
if ($this->parentView || $this->layout) {
$content = ($this->parentView ?? $this->layout)
->setSections(array_merge($this->sections, ['content' => $content]))
->render();
}
return $content;
......@@ -141,7 +146,7 @@ class View
*/
public function extend(string $name, array $data = null)
{
$this->parentTemplate = $this->factory->view($name, $data ?? $this->data);
$this->parentView = $this->factory->view($name, $data ?? $this->data, $this->layout);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment