diff --git a/app/core/template/Output.php b/app/core/template/Output.php old mode 100755 new mode 100644 index e454fc0..bb895cf --- a/app/core/template/Output.php +++ b/app/core/template/Output.php @@ -2,92 +2,125 @@ namespace App\Core\Template; -class Output { +use Mustache_Engine as Mustache_Engine; +use Mustache_Loader_FilesystemLoader as Mustache_Loader_FilesystemLoader; + +class Output +{ private static $_instance; - private $view = false; - protected $values = Array(); - private $_template = ''; private $_extension = ''; - private function __construct() { - $this->_template = ( defined('TEMPLATE_DEFAULT') ) ? TEMPLATE_DEFAULT : 'DefaultTemplate'; - $this->_extension = ( defined('TEMPLATE_FILETYPE') ) ? TEMPLATE_FILETYPE : '.mustache'; + private $_engine; + + private function __construct() + { + $this->_template = (defined('TEMPLATE_DEFAULT')) ? TEMPLATE_DEFAULT : 'DefaultTemplate'; + $this->_extension = (defined('TEMPLATE_FILETYPE')) ? TEMPLATE_FILETYPE : '.mustache'; + + $this->_engine = new Mustache_Engine(array( + 'entity_flags' => ENT_QUOTES, + 'partials_loader' => new Mustache_Loader_FilesystemLoader(DIR_APP), + 'pragmas' => [Mustache_Engine::PRAGMA_FILTERS], + )); + + // ======================== + // Helpers + // + + // Format {{#icons}}xx{{/icons}} as a font awesome icon + $this->_engine->addHelper('icons', function ($icon) { + return ""; + }); + + // Change the {{#strings}}string,module{{/strings}} for the requested localized string + $this->_engine->addHelper('string', function ($string) { + return \Lang::getString(...explode(',', trim($string))); + }); + + // ======================== + // Filters + // + + $this->_engine->addHelper('case', [ + 'lower' => function ($value) { + return strtolower((string) $value); + }, + 'upper' => function ($value) { + return strtoupper((string) $value); + } + ]); } - private static function newObj() { + private static function newObj() + { if (!isset(self::$_instance)) { self::$_instance = new Output(); } return self::$_instance; } - public function getInstance() { + public static function getInstance() + { if (!isset(self::$_instance)) { return self::newObj(); } return self::$_instance; } - public function setTemplate($template = 'DefaultTemplate') { + // + // ---------------------------------------- + // Done with singletone and template boot + // + // + + public static function setTemplate($template = 'DefaultTemplate') + { self::getInstance()->_template = $template; -// self::getInstance()->_template = ''; return self::getInstance(); } - public static function renderView($name, $values = null, $location = null) { - if (!$location) { - $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']); - array_pop($segments); - $location = implode('/', $segments) . '/views/'; - } - - self::getInstance()->setView($name, $values, $location)->render(); - } - - public static function setView($name = '', $values = Array(), $location = null) { - if($name == ''){ return self::getInstance(); } - $instance = self::getInstance(); - $folder = ''; - if (!$location) { - $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']); - array_pop($segments); - $location = implode('/', $segments) . '/views/'; - } - $folder = $location; - $location .= $name; - - if (strpos($instance->_extension, $location) == 0) { - $location .= $instance->_extension; - } - - $instance->view = Array(); - $instance->view['location'] = $location; - $instance->view['folder'] = $folder; - $instance->view['values'] = $values; - return self::getInstance(); - } - - public static function addValue($key, $value){ - self::getInstance()->view['values'][$key] = $value; - } - - public static function render() { + public static function render($view = '', $values = array()) + { $instance = self::getInstance(); - if(!is_file(__DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php') ){ + $template = ''; + + if ($view != '') { + $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']); + array_pop($segments); + $location = implode('/', $segments) . '/views/'; + + if (is_file($location . $view . $instance->_extension)) { + $template = file_get_contents($location . $view . $instance->_extension); + } + } + + if (is_ajax_request()) { + echo $instance->_engine->render($template, $values); + return; + } + + if (!is_file(__DIR__ . "/classes/" . strtolower($instance->_template) . "/" . $instance->_template . '.php')) { $instance->_template = 'DefaultTemplate'; } - include_once __DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php'; - - $class = new $instance->_template; - $class->setView($instance->view)->render(); + include_once __DIR__ . "/classes/" . strtolower($instance->_template) . "/" . $instance->_template . '.php'; + $wrapper = new $instance->_template; + $wrapper = $wrapper->render($template); + echo $instance->_engine->render($wrapper, $values); } + + // + // ---------------------------------------- + // Menu Stuff Here + // + // + public static function addMenu($route, $name, $icon = '', $attributes = [], $weight = 0){ global $SIDEBAR; $SIDEBAR->add($route, $name, $icon, $attributes, $weight); @@ -103,8 +136,6 @@ class Output { $SIDEBAR->add_on_new($key, $route, $name, $icon, $attributes, $weight); } - //======== - public static function headAdd($route, $name){ global $DROPDOWN; $DROPDOWN[] = ['route' => $route, 'name' => $name]; @@ -119,10 +150,4 @@ class Output { global $DROPDOWN; $DROPDOWN->add_on_new($key, $route, $name, $icon, $attributes, $weight); } - - - public static function icon($icon){ - return ""; - } - } diff --git a/app/core/template/Routes.php b/app/core/template/Routes.php old mode 100755 new mode 100644 index 7c04894..044969e --- a/app/core/template/Routes.php +++ b/app/core/template/Routes.php @@ -16,4 +16,7 @@ RouteCollection::get('*', function(){ //$DROPDOWN = new Menu( array("class" => 'dropdown-menu') ); $DROPDOWN = []; -}, -11)->doIgnore(); +}, -12)->doIgnore(); + + +RouteCollection::get('/template/[r:location]', '\App\Core\Template\Template@getTemplate'); \ No newline at end of file diff --git a/app/core/template/Template.php b/app/core/template/Template.php new file mode 100644 index 0000000..c7cd4eb --- /dev/null +++ b/app/core/template/Template.php @@ -0,0 +1,18 @@ +_template = ( defined('TEMPLATE_DEFAULT') ) ? TEMPLATE_DEFAULT : 'DefaultTemplate'; - $this->_extension = ( defined('TEMPLATE_FILETYPE') ) ? TEMPLATE_FILETYPE : '.mustache'; - } - - private static function newObj() { - if (!isset(self::$_instance)) { - self::$_instance = new Output(); - } - return self::$_instance; - } - - public function getInstance() { - if (!isset(self::$_instance)) { - return self::newObj(); - } - return self::$_instance; - } - - public function setTemplate($template = 'DefaultTemplate') { - self::getInstance()->_template = $template; -// self::getInstance()->_template = ''; - return self::getInstance(); - } - - public static function renderView($name, $values = null, $location = null) { - if (!$location) { - $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']); - array_pop($segments); - $location = implode('/', $segments) . '/views/'; - } - - self::getInstance()->setView($name, $values, $location)->render(); - } - - public static function setView($name = '', $values = Array(), $location = null) { - if($name == ''){ return self::getInstance(); } - $instance = self::getInstance(); - $folder = ''; - if (!$location) { - $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']); - array_pop($segments); - $location = implode('/', $segments) . '/views/'; - } - $folder = $location; - $location .= $name; - - if (strpos($instance->_extension, $location) == 0) { - $location .= $instance->_extension; - } - - $instance->view = Array(); - $instance->view['location'] = $location; - $instance->view['folder'] = $folder; - $instance->view['values'] = $values; - return self::getInstance(); - } - - public static function addValue($key, $value){ - self::getInstance()->view['values'][$key] = $value; - } - - public static function render() { - $instance = self::getInstance(); - - if(!is_file(__DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php') ){ - $instance->_template = 'DefaultTemplate'; - } - - include_once __DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php'; - - $class = new $instance->_template; - $class->setView($instance->view)->render(); - - } - - public static function addMenu($route, $name, $icon = '', $attributes = [], $weight = 0){ - global $SIDEBAR; - $SIDEBAR->add($route, $name, $icon, $attributes, $weight); - } - - public static function addSubmenu($key, $name, $icon = '', $attributes = [], $weight = 0){ - global $SIDEBAR; - $SIDEBAR->create_submenu($key, $name, $icon, $attributes , $weight); - } - - public static function addOnSubmenu($key, $route, $name, $icon = '', $attributes = [], $weight = 0){ - global $SIDEBAR; - $SIDEBAR->add_on_new($key, $route, $name, $icon, $attributes, $weight); - } - - //======== - - public static function headAdd($route, $name){ - global $DROPDOWN; - $DROPDOWN[] = ['route' => $route, 'name' => $name]; - } - - public static function addSubHeader($key, $name, $icon = '', $attributes = [], $weight = 0){ - global $DROPDOWN; - $DROPDOWN->create_submenu($key, $name, $icon, $attributes , $weight); - } - - public static function addOnSubHeader($key, $route, $name, $icon = '', $attributes = [], $weight = 0){ - global $DROPDOWN; - $DROPDOWN->add_on_new($key, $route, $name, $icon, $attributes, $weight); - } - - - public static function icon($icon){ - return ""; - } - -} diff --git a/app/core/template/classes/dashboard/Dashboard.php b/app/core/template/classes/dashboard/Dashboard.php old mode 100755 new mode 100644 index 0d2bae3..61dbc8a --- a/app/core/template/classes/dashboard/Dashboard.php +++ b/app/core/template/classes/dashboard/Dashboard.php @@ -6,10 +6,10 @@ class Dashboard extends RenderableTemplate { public $content; - public function render() { + public function render($content = '') { global $DROPDOWN, $SIDEBAR; - if (is_ajax_request()) { + /*if (is_ajax_request()) { if(is_file($this->getView()['location'])){ $this->content = file_get_contents($this->getView()['location']); $this->implate(); @@ -18,36 +18,54 @@ class Dashboard extends RenderableTemplate { $this->implate(); } return; - } + }*/ //global $DROPDOWN, $SIDEBAR; $head = ''; - $head .= ''; + $head .= ''; $head .= ''; - $head .= ''; + $head .= ''; $head .= ''; $head .= ''; - $head .= ''; + $head .= ''; + + // Trumbowyg WYSIWYG + $head .= ''; + $head .= ''; + + $head .= ''; - $head .= ''; $head .= ''; $head .= ''; - $head .= ''; - $head .= ''; + $head .= ''; + $head .= ''; - $head .= ''; - $head .= ''; + $head .= ''; + $head .= ''; + + $head .= ''; + $head .= ''; + + $head .= ''; + + $head .= ''; + + $head .= ''; + $head .= ''; + + $head .= ''; + $head .= ''; $foot = ''; - $foot .= ''; + $foot .= ''; $this->content = file_get_contents(__DIR__ . '/template.html'); @@ -60,19 +78,14 @@ class Dashboard extends RenderableTemplate { foreach ($DROPDOWN as $item){ $header .= "".$item['name'].""; } + $this->content = str_replace("{{dropdown}}", $header, $this->content); $this->content = str_replace("{{head}}", $head, $this->content); - - if(isset($this->getView()['location']) && is_file($this->getView()['location'])){ - $this->content = str_replace('{{>content}}', $this->replaceSections(file_get_contents($this->getView()['location']), $this->_view['values']), $this->content); - }else{ - $this->content = str_replace('{{>content}}', '', $this->content); - } - $this->content = str_replace("{{footer}}", $foot, $this->content); + $this->content = str_replace('{{content}}', $content, $this->content); - $this->implate(); + return $this->content; } diff --git a/app/core/template/classes/dashboard/template.html b/app/core/template/classes/dashboard/template.html old mode 100755 new mode 100644 index 945ace5..c0dd3fa --- a/app/core/template/classes/dashboard/template.html +++ b/app/core/template/classes/dashboard/template.html @@ -1,78 +1,97 @@ -
- - - {{head}} - - -