From 80530127310658c205ec1b30e807359ab2357d85 Mon Sep 17 00:00:00 2001 From: Fariz Luqman Date: Sat, 25 Mar 2017 23:19:08 +0800 Subject: [PATCH 1/2] Add new tempting feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow callback, and access to another object’s property from the template file --- src/Viewer.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Viewer.php b/src/Viewer.php index 1abd612..a1b618f 100755 --- a/src/Viewer.php +++ b/src/Viewer.php @@ -105,15 +105,34 @@ 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]]; + // !TODO if '.' is found in the string, assume it is an object + // and return the property of the object + + // else, return the value of the variable + if (strpos($matches[1], '.') !== false) { + list($object, $property) = explode('.', $matches[1]); + + // if a '()' is found in $property, change it to callable + if (strpos($property, '()') !== false) { + list($function, $parenthesis) = explode('()', $property); + return(self::$hive[$object]->$function()); + }else{ + return(self::$hive[$object]->$property); + } + + }else{ + if(isset(self::$hive[$matches[1]])){ + return self::$hive[$matches[1]]; + } } + } -} +} \ No newline at end of file From d5ab2aae2557dda3709cb31f05947e45b693d2ec Mon Sep 17 00:00:00 2001 From: Fariz Luqman Date: Sat, 25 Mar 2017 23:38:51 +0800 Subject: [PATCH 2/2] Viewer Commenting --- src/Viewer.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Viewer.php b/src/Viewer.php index a1b618f..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); @@ -112,21 +116,27 @@ static private function render($file, $data){ } static private function replace($matches) { - // !TODO if '.' is found in the string, assume it is an object - // and return the property of the object + // If '.' is found in the $matches[1], assume it is an object + // which have a property - // else, return the value of the variable + // 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, change it to callable + // 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]];