Skip to content

Commit

Permalink
Added templating engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Fariz Luqman committed Feb 24, 2017
1 parent 829772c commit cc60bec
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
*/
class Viewer {

private static $hive;

/**
* Finds, renders and displays a template file. Reports a 404 error in
* case of missing files.
Expand Down Expand Up @@ -103,11 +105,23 @@ static private function render($file, &$data = []){
extract(Sharer::get());
}

// Get all defined vars into $hive, which is used in callback method replace
self::$hive = get_defined_vars();

ob_start();
include_once($file);
$output = ob_get_contents();
include($file);
$input = ob_get_contents();
ob_end_clean();
echo ($output);

$output = preg_replace_callback('!\{\{(\w+)\}\}!', 'Viewer::replace', $input);

echo($output);
}

static private function replace($matches) {
if(isset(self::$hive[$matches[1]])){
return self::$hive[$matches[1]];
}
}

}

0 comments on commit cc60bec

Please sign in to comment.