diff --git a/application/helpers/component_helper.php b/application/helpers/component_helper.php index fae6c50d..2285e712 100644 --- a/application/helpers/component_helper.php +++ b/application/helpers/component_helper.php @@ -43,3 +43,78 @@ if ( ! function_exists('component')) return $CI->load->view('components/' . $component, $vars, TRUE); } } + +if ( ! function_exists('extend')) +{ + /** + * Use this function at the top of view files to mark the layout you are extending from. + * + * @param $layout + */ + function extend($layout) + { + config([ + 'layout' => [ + 'filename' => $layout, + 'sections' => [], + ] + ]); + } +} + +if ( ! function_exists('section')) +{ + /** + * Use this function in view files to mark the beginning and/or end of a layout section. + * + * Sections will only be used if the view file extends a layout and will be ignored otherwise. + * + * Example: + * + * + * + * + * + *
This is the content of the section.
+ * + * + * + * + * + * @param string $name + */ + function section(string $name) + { + $layout = config('layout'); + + if (array_key_exists($name, $layout['sections'])) + { + $layout['sections'][$name] = ob_get_clean(); + + config(['layout' => $layout]); + + return; + } + + $layout['sections'][$name] = ''; + + config(['layout' => $layout]); + + ob_start(); + } +} + +if ( ! function_exists('slot')) +{ + /** + * Use this function in view files to mark a slot that sections can populate from within child templates. + * + * @param string $name + */ + function slot(string $name) + { + $layout = config('layout'); + + echo $layout['sections'][$name]; + } +}