diff --git a/src/Viewer.php b/src/Viewer.php index 1abd612..bcb4582 100755 --- a/src/Viewer.php +++ b/src/Viewer.php @@ -41,6 +41,8 @@ */ class Viewer { + // the hive is where all data is stored, which is then usable from all template + // files private static $hive = []; /** @@ -97,6 +99,8 @@ static private function render($file, $data){ if(Sharer::get() !== null){ extract(Sharer::get()); } + + // Merge data into the hive self::$hive = array_merge(self::$hive, get_defined_vars()); unset($data); @@ -105,15 +109,40 @@ static private function render($file, $data){ $input = ob_get_contents(); ob_end_clean(); - $output = preg_replace_callback('!\{\{(\w+)\}\}!', 'Viewer::replace', $input); + $output = preg_replace_callback('!\{\{(.*?)\}\}!', 'Viewer::replace', $input); + echo($output); } static private function replace($matches) { - if(isset(self::$hive[$matches[1]])){ - return self::$hive[$matches[1]]; + // If '.' is found in the $matches[1], assume it is an object + // which have a property + + // else, assume it is a variable + if (strpos($matches[1], '.') !== false) { + // explode the part before and after '.' + // the part before '.' is an object, while the part after '.' is a property + list($object, $property) = explode('.', $matches[1]); + + // if a '()' is found in $property, we will then assume it to be a callable + // method. + if (strpos($property, '()') !== false) { + // remove paranthesis + list($function, $parenthesis) = explode('()', $property); + + // return the callable method of the object from the hive + return(self::$hive[$object]->$function()); + }else{ + // return the property of the object from the hive + return(self::$hive[$object]->$property); + } + }else{ + if(isset(self::$hive[$matches[1]])){ + return self::$hive[$matches[1]]; + } } + } -} +} \ No newline at end of file