From cc60becfdbee6afd4f911500f389a201a4cc8413 Mon Sep 17 00:00:00 2001 From: Fariz Luqman Date: Fri, 24 Feb 2017 14:04:55 +0800 Subject: [PATCH] Added templating engine --- src/Viewer.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Viewer.php b/src/Viewer.php index d4ce661..6088067 100755 --- a/src/Viewer.php +++ b/src/Viewer.php @@ -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. @@ -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]]; + } + } + }