Example #1
0
 /**
  * render the theme or views.
  * 
  * @param string $views_file path to theme file or views file.
  * @param array $output_data the data that will be sent to theme or views.
  */
 public function render($views_file, array $output_data = [])
 {
     // get the module name of this views.
     // the $module_name can be just name and folder/name. for example: Cms = Modules\Cms, System/Core = System\Core.
     $Modules = new \System\Libraries\Modules();
     $module_name = $Modules->currentModule();
     unset($Modules);
     // init config library to load theme config file.
     $config = new \System\Libraries\Config();
     $config->load('theme');
     // get the theme name.
     if (strpos($views_file, 'admin/') !== false) {
         $fallback_type = 'admin';
     } else {
         $fallback_type = 'front';
     }
     $theme_name = $this->getThemeNameWithFallback($config, $fallback_type);
     $theme_name_fallback = $this->getThemeFallback($config, $fallback_type);
     unset($fallback_type);
     $output = '';
     // check template engine enabled.
     $template_engine = $config->get('template_engine', 'theme');
     $theme_dir = $config->get('theme_dir', 'theme');
     if ($template_engine != null) {
         // theme config uses template engine.
         $TemplateClass = call_user_func($template_engine);
         $theme_location = $this->findThemeOrViewsLocation($views_file, $theme_dir, $theme_name, $theme_name_fallback, $module_name, $TemplateClass);
         if (is_array($theme_location) && array_key_exists('theme_path', $theme_location) && array_key_exists('theme_file', $theme_location) && array_key_exists('use_template', $theme_location) && array_key_exists('theme_name', $theme_location)) {
             // can find template location.
             if ($theme_location['use_template'] === true) {
                 // confirmed use template file.
                 // use load template and render it.
                 $output = $TemplateClass->render($theme_location['theme_path'], $theme_location['theme_file'], $theme_location['theme_name'], $module_name, $output_data);
             } else {
                 // cannot confirmed use template file.
                 // use normal load views file.
                 $output = $this->loadViews($theme_location['theme_path'] . '/' . $theme_location['theme_file'], $output_data);
             }
         } else {
             // cannot find template location.
             // use normal load views file.
             $output = $this->loadViews($theme_location['theme_path'] . '/' . $theme_location['theme_file'], $output_data);
         }
         unset($TemplateClass);
     } else {
         // theme config do not use template engine.
         $views_location = $this->findThemeOrViewsLocation($views_file, $theme_dir, $theme_name, $theme_name_fallback, $module_name);
         $output = $this->loadViews($views_location['theme_path'] . '/' . $views_location['theme_file'], $output_data);
     }
     unset($config, $module_name, $template_engine, $theme_dir, $theme_location, $theme_name, $theme_name_fallback, $views_location);
     return $output;
 }