I have the code below where my variable $layout
loses it’s value and I have no idea why, the layoutContent
is just a method that get the template of my website, then
renderOnlyView
get the view supposed to render in that template
$layout = $this->layoutContent($layoutParams);
echo ($layout);
$view = $this->renderOnlyView($view, $params);
echo ($layout);
the whole method where the bug occurred
public function renderView(string $view, array $params = [], array $layoutParams = [])
{
$layout = $this->layoutContent($layoutParams);
echo ($layout);
$view = $this->renderOnlyView($view, $params);
echo ($layout);
$layout = str_replace('{{ title }}', $layoutParams['title'] ?? 'No title', $layout);
return str_replace('{{ body }}', $view, $layout);
}
and renderOnlyView
protected function renderOnlyView(string $view, array $params): ?string
{
foreach ($params as $key => $param) {
$$key = $param;
}
ob_start();
$viewFilePath = Application::$ROOT_DIR . "/views/$view.blade.php";
if (file_exists($viewFilePath))
include_once $viewFilePath;
else
echo "the view <b>$view</b> is not found";
return ob_get_clean();
}
layoutContent
just in case
public function layoutContent(array $params = [])
{
$layout = Application::$APP->controller->layout ?? 'main';
ob_start();
include_once Application::$ROOT_DIR . "/views/layout/$layout.layout.php";
$output = ob_get_clean();
foreach ($params as $key => $param) {
$output = str_replace('{{ ' . $key . ' }}', $param, $output);
}
return $output;
}
more infos: PHP Version: 7.4.3, system ubuntu 20.04
2
Answers
Okay guys problem found, i missed a point, this function get called two times, the first time when I'm rendering my view, the second time when my view throw an error and while catching it and trying to display the
renderLayout
get called again, causing the include_once to return null since it was already included :DU missed a point, this function get called two times, the first time when you rendering my view, the second time when you view throw an error and while catching it and trying to display the renderLayout get called again, causing the include_once to return null since it was already included