Allow multiple renders with the loader class during the rendering of a layout page

This commit is contained in:
alextselegidis 2021-11-17 08:41:51 +01:00
parent 35f9a6eb1c
commit 532ee4fabc
2 changed files with 13 additions and 23 deletions

View file

@ -41,21 +41,25 @@
*/ */
class EA_Loader extends CI_Loader { class EA_Loader extends CI_Loader {
/** /**
* Override the original view loader method so that layouts are also supported. * Override the original view loader method so that layouts are also supported.
* *
* @param string $view View filename. * @param string $view View filename.
* @param array $vars An associative array of data to be extracted for use in the view. * @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. * @param bool $return Whether to return the view output or leave it to the Output class.
* *
* @return object|string * @return object|string
*/ */
public function view($view, $vars = [], $return = FALSE) public function view($view, $vars = [], $return = FALSE)
{ {
$layout = config('layout');
$is_layout_page = empty($layout); // This is a layout page if "layout" was undefined before the page got rendered.
$result = $this->_ci_load(['_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return]); $result = $this->_ci_load(['_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return]);
$layout = config('layout'); $layout = config('layout');
if ($layout) if ($layout && $is_layout_page)
{ {
$result = $this->_ci_load(['_ci_view' => $layout['filename'], '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return]); $result = $this->_ci_load(['_ci_view' => $layout['filename'], '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return]);
} }

View file

@ -32,30 +32,16 @@ if ( ! function_exists('component'))
* *
* @return string Return the HTML if the $return argument is TRUE or NULL. * @return string Return the HTML if the $return argument is TRUE or NULL.
*/ */
function component(string $component, string $attributes = '', array $params = [], bool $return = FALSE): ?string function component(string $component, string $attributes = '', array $params = [], bool $return = FALSE)
{ {
/** @var EA_Controller $CI */
$CI = get_instance();
$vars = array_merge($params, [ $vars = array_merge($params, [
'attributes' => $attributes 'attributes' => $attributes
]); ]);
extract($vars); return $CI->load->view('components/' . $component, $vars, $return);
ob_start();
require APPPATH . 'views/components/' . $component . '.php';
$html = ob_get_clean();
if ($return)
{
return $html;
}
else
{
echo $html;
return NULL;
}
} }
} }