tpl = $contents; } } /** * Merges new variables into the template replacement variables array. * * @param mixed[] $vars Array of new template replacement variables. */ public function register_vars($vars) { $this->tpl_vars = array_merge($this->tpl_vars, $vars); } /** * Renders the template by replacing variables and storing the result * in the buffer. * * Optionally, one can pass an array of template replacement variables. * * @param mixed[] $vars Array of new template replacement variables. */ public function render($vars=array()) { if (!empty($vars)) { $this->register_vars($vars); } $this->buffer = $this->var_repl($this->tpl, $this->tpl_vars); } /** * Replaces template variables with their values, as taken * from the $tpl_vars array. * * @param string $tpl Template to decorate. * @param mixed[] $vars Array of replacement variables. */ private function var_repl($tpl, $vars) { foreach ($vars as $k => $v) { $tpl = str_replace('{{$'.$k.'}}', $v, $tpl); } return $tpl; } /** * Returns the rendered template file. * * @return string The current output buffer. */ public function get_buffer() { return $this->buffer; } /** * Outputs the rendered template file. */ public function output_buffer($str) { print($this->buffer); } }