Skip to content

Commit

Permalink
Add new tempting feature
Browse files Browse the repository at this point in the history
Allow callback, and access to another object’s property from the
template file
  • Loading branch information
farizluqman committed Mar 25, 2017
1 parent cd17bb0 commit 8053012
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}
}

}

}
}

0 comments on commit 8053012

Please sign in to comment.