Support multiple additions of the same section content

This commit is contained in:
Alex Tselegidis 2022-01-11 10:11:09 +01:00
parent cd2fe86aa3
commit 0a005d570b

View file

@ -28,19 +28,19 @@ if ( ! function_exists('component'))
* @param string $component Component template file name. * @param string $component Component template file name.
* @param string $attributes HTML attributes for the parent component element. * @param string $attributes HTML attributes for the parent component element.
* @param array $params Additional parameters for the component. * @param array $params Additional parameters for the component.
* @param bool $return Whether to return the HTML or echo it directly. * @param bool $return Whether to return the HTML or echo it directly.
* *
* @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) function component(string $component, string $attributes = '', array $params = [], bool $return = FALSE)
{ {
/** @var EA_Controller $CI */ /** @var EA_Controller $CI */
$CI = get_instance(); $CI = get_instance();
$vars = array_merge($params, [ $vars = array_merge($params, [
'attributes' => $attributes 'attributes' => $attributes
]); ]);
return $CI->load->view('components/' . $component, $vars, $return); return $CI->load->view('components/' . $component, $vars, $return);
} }
} }
@ -90,14 +90,14 @@ if ( ! function_exists('section'))
if (array_key_exists($name, $layout['sections'])) if (array_key_exists($name, $layout['sections']))
{ {
$layout['sections'][$name] = ob_get_clean(); $layout['sections'][$name][] = ob_get_clean();
config(['layout' => $layout]); config(['layout' => $layout]);
return; return;
} }
$layout['sections'][$name] = ''; $layout['sections'][$name] = [];
config(['layout' => $layout]); config(['layout' => $layout]);
@ -116,6 +116,16 @@ if ( ! function_exists('slot'))
{ {
$layout = config('layout'); $layout = config('layout');
echo $layout['sections'][$name] ?? ''; $section = $layout['sections'][$name] ?? NULL;
if ( ! $section)
{
return;
}
foreach ($section as $content)
{
echo $content;
}
} }
} }