tsz Posted April 24, 2015 Share Posted April 24, 2015 (edited) Soo I try to build an simple and basic template engine which displays content from template files. here is the class: class template { public $filename; public $assigned_vars = array(); public function assign($key, $value) { $this->assigned_vars[$key] = $value; } public function display($filename) { if(file_exists($filename)) { $output = file_get_contents($filename); foreach($this->assigned_vars as $key => $value) { $output = preg_replace('/{'.$key.'}/', $value, $output); } return $output; } else{ return "Missing template error"; } } } yet when I tries to execute the class its doesn't return anything. i execute it like this: $tp->display(ROOT_PATH.'home.php'); consider that I have defined ROOT_PATH already and $tp as new template class. what have i done wrong? help plz Edited April 24, 2015 by tsz Quote Link to comment https://forums.phpfreaks.com/topic/295834-template-class-problem/ Share on other sites More sharing options...
cyberRobot Posted April 24, 2015 Share Posted April 24, 2015 (edited) It's probably not this simple, but have you tried displaying the returned result? print $tp->display(ROOT_PATH.'home.php'); Edited April 24, 2015 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/295834-template-class-problem/#findComment-1509841 Share on other sites More sharing options...
tsz Posted April 24, 2015 Author Share Posted April 24, 2015 I have tried but it doesn't work either. Quote Link to comment https://forums.phpfreaks.com/topic/295834-template-class-problem/#findComment-1509842 Share on other sites More sharing options...
cyberRobot Posted April 24, 2015 Share Posted April 24, 2015 Are all PHP errors and warnings enabled? Are they being shown? Note that you can add the following to the top of your script during the debugging process: error_reporting(E_ALL); ini_set('display_errors', 1); Also, have you tried adding some debugging statements to your class to know if things are even being called? For example, you could try something like this: <?php public function display($filename) { print '<div>In display() method</div>'; if(file_exists($filename)) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/295834-template-class-problem/#findComment-1509845 Share on other sites More sharing options...
tsz Posted April 24, 2015 Author Share Posted April 24, 2015 Are all PHP errors and warnings enabled? Are they being shown? Note that you can add the following to the top of your script during the debugging process: error_reporting(E_ALL); ini_set('display_errors', 1); Also, have you tried adding some debugging statements to your class to know if things are even being called? For example, you could try something like this: <?php public function display($filename) { print '<div>In display() method</div>'; if(file_exists($filename)) { ?> I have tried too add the error reporting script. no related warnings to the class(only to another, unrelevent class). your second suggestion doesn't output anything either. Quote Link to comment https://forums.phpfreaks.com/topic/295834-template-class-problem/#findComment-1509850 Share on other sites More sharing options...
cyberRobot Posted April 24, 2015 Share Posted April 24, 2015 You could try adding the following debugging statement to your code right before you attempt to call the display() method: print '<pre>' . print_r($tp, true) . '</pre>'; Does it say that $tp is a "template Object"? Quote Link to comment https://forums.phpfreaks.com/topic/295834-template-class-problem/#findComment-1509853 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.