Example #1
0
 /**
  This functions loads the data related to each page or layout
  for a page we load all files in the related directory
  for a layout we check the "datas-sources" attribute of our layout_file.json and load them
  In your real world application you can load data only when it is needed according to each page inside controllers
 */
 private function load_data($data_dir)
 {
     $data_files = array();
     $format = "/([0-9a-z\\-_]+)\\.(json|csv)\$/i";
     //see if there's a folder for partial data relating to this page or layout
     //if so iterate all json/csv files and load the data
     $partial_data_folder = "{$data_dir}/{$this->type}s/partials/{$this->name}";
     $stats = null;
     if (is_dir($partial_data_folder)) {
         $files = scandir($partial_data_folder);
         foreach ($files as $name) {
             if ($name == '.' or $name == '..') {
                 continue;
             }
             if (!preg_match($format, $name, $filename)) {
                 continue;
             }
             $data_files[$filename[1]] = "{$partial_data_folder}/{$name}";
         }
     }
     foreach ($data_files as $var_name => $var_value) {
         $new_data = '';
         if (preg_match("/\\.json\$/i", $data_files[$var_name])) {
             $new_data = json_decode(file_get_contents($data_files[$var_name]), TRUE);
         } else {
             if (preg_match("/\\.csv\$/i", $data_files[$var_name])) {
                 //load csv data into an array
                 $new_data = CSV::to_array($data_files[$var_name]);
                 foreach ($new_data as &$data) {
                     if (isset($data['status'])) {
                         $data[$data['status']] = true;
                     }
                 }
             }
         }
         $this->vars[str_replace('.', '_', $var_name)] = $new_data;
         //here we replace '.' with '_' in variable names, so template compiler can recognize it as a variable not an object
         //for example change sidebar.navList to sidebar_navList , because sidebar is not an object
     }
     return true;
 }