karpatzio Posted May 19, 2008 Share Posted May 19, 2008 I wrote a simple template class for educational purposes (im a total newbee to php and programming in general) thats based on an article i read, anyway unlike the article i added a member variable $template to store the parsed data instead of storing it in the body of the application. i doubt either way would have areal impact in a simple app using this class but still i was wondering why did the author chose to handle the data in the body for the sake of simplicity in a code meant to be delivered to the unexperienced such as me or if it is by principle more efficient/correct? the code: class mTemplate { private var $args; private var $fileRoot; private var $template; // Holds the template to be written /******************************************************************* Constructor *******************************************************************/ function __construct($templateDir = NULL) { $this->fileRoot = $templateDir; $this->args = array(); $this->template = ''; } /******************************************************************* Public Methods *******************************************************************/ public function setPath($fileRoot) { $this->fileRoot = $fileRoot; } public function setVars($var, $arg = NULL) { if(is_array($var)) { if($arg) { $this->args = $var; } else { $this->args = array_merge($this->args, $var); } } else { $this->arg['$var'] = $arg; } } public function returnParsed($file, $args = NULL) { if(is_array($args)) { extract($args); } else { extract($this->args); } ob_start(); include($this->fileRoot . $file); $contents = ob_get_contents(); ob_end_clean(); // Close buffer, dont send to client return $contents; } public function addParsed($file, $args = NULL) { if(is_array($args) { extract($args); } else { extract($this->args); } ob_start(); include($this->fileRoot . $file); $contents = ob_get_contents(); ob_end_clean(); // Close buffer, dont send to client $this->template .= $contents; } public function addUnparsed($bit) { if(file_exists($this->fileRoot . $bit)) { $this->template .= file_get_contents($this->fileRoot . $bit); } else { $this->template .= $bit; } } public function write() { echo $this->template; } public function get() { return $this->template; } public function delete() { $this->template = ''; } } Quote Link to comment 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.