From b7bcaa86bebeabec0767e87e62ebf785b4510ef4 Mon Sep 17 00:00:00 2001 From: alextselegidis Date: Mon, 15 Nov 2021 08:53:59 +0100 Subject: [PATCH] Override the "view" method of the CI_Loader class so that layouts are supported --- application/core/EA_Loader.php | 39 ++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/application/core/EA_Loader.php b/application/core/EA_Loader.php index 519c68b9..346bcab2 100644 --- a/application/core/EA_Loader.php +++ b/application/core/EA_Loader.php @@ -44,22 +44,47 @@ class EA_Loader extends CI_Loader { * Loads "layout" files. * * @param string $layout Path to the layout view file. - * @param string $page Path to the page view file. + * @param string $page Path to the page view file. * @param array $vars An associative array of data to be extracted for use in the view * @param bool $return Whether to return the view output or leave it to the Output class * * @return object|string + * + * remove this */ public function layout(string $layout, string $page, array $vars = [], bool $return = FALSE) { $vars['page_path'] = APPPATH . 'views/' . $page . '.php'; - - $vars['styles'] = $vars['styles'] ?? []; - - $vars['scripts'] = $vars['scripts'] ?? []; - - $vars['global_variables'] = $vars['global_variables'] ?? []; + + $vars['styles'] = $vars['styles'] ?? []; + + $vars['scripts'] = $vars['scripts'] ?? []; + + $vars['global_variables'] = $vars['global_variables'] ?? []; return $this->view($layout, $vars, $return); } + + /** + * Override the original view loader method so that layouts are also supported. + * + * @param string $view View filename. + * @param array $vars An associative array of data to be extracted for use in the view. + * @param bool $return Whether to return the view output or leave it to the Output class. + * + * @return object + */ + public function view($view, $vars = [], $return = FALSE): object + { + $result = $this->_ci_load(['_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return]); + + $layout = config('layout'); + + if ($layout) + { + $result = $this->_ci_load(['_ci_view' => $layout, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return]); + } + + return $result; + } }