diff --git a/src/mite.php b/src/mite.php index 45626d1..6d8f00e 100644 --- a/src/mite.php +++ b/src/mite.php @@ -37,6 +37,9 @@ class Mite { // initializes framework public function __construct($config = []) { + // define base path as calling + define('BASE', dirname(debug_backtrace()[0]['file'])); + $this->init($config); } @@ -46,21 +49,47 @@ class Mite { self::$init = microtime(TRUE); // sensible defaults - $defaults = array - ( - 'index' => '/index.php', - 'debug' => 'FALSE', - 'paths' => array( - 'views' => '', - 'models' => '' - ) - ); + $defaults = [ + 'index' => '/index.php', + 'debug' => FALSE, + 'layout' => FALSE, + 'paths' => $this->paths($config['paths']) + ]; // merge in configuration overriding defaults self::$options = array_merge($defaults, $config); // TODO: ack!!! forgot to update/retrieve settings from options - self::$index = self::options('index'); + // self::$index = self::options('index'); + } + + private function paths($config = []) + { + // application default paths + $defaults = [ + 'views' => '', + 'layouts' => '', + 'models' => '' + ]; + + // check views/ structure + if(file_exists(BASE . '/views')) { + // set views directory + $defaults['views'] = BASE . '/views'; + + // check for layouts + if(file_exists(BASE . '/views/layouts')) { + // set layouts directory + $defaults['layouts'] = BASE . '/views/layouts'; + } + } + + // check and set models directory + if(file_exists(BASE . '/models')) { + $defaults['models'] = BASE . '/models'; + } + + return array_merge($defaults, $config['paths'] ?: []); } /** @@ -258,10 +287,19 @@ class Mite { // check if folder defined $dir = self::options('paths','views'); - // update path - $path = $dir ? "{$dir}/{$template}" : "{$template}"; + // use layout file + if(file_exists("{$dir}/layouts/index.layout.php")) { - include($path); + $content = $dir ? "{$dir}/{$template}" : "{$template}"; + + include("{$dir}/layouts/index.layout.php"); + + } else { + // determine path to view template + $path = $dir ? "{$dir}/{$template}" : "{$template}"; + + include($path); + } } /*****************************************************************