| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- class View {
- // Path to templates dir
- private $path = 'templates';
- // Name of template, default: default
- private $template = 'default';
- // Stores variables for templates
- private $_ = array();
- /**
- * Assign a key/val pair for the template
- *
- * @param String $key Key
- * @param String $value Variable
- */
- public function assign($key, $value) {
- $this->_[$key] = $value;
- }
- /**
- * Sets templates name
- *
- * @param String $template Name of template
- */
- public function setTemplate($template = 'default') {
- $this->template = $template;
- }
- /**
- * Load template and return result
- *
- * @return string Output of template
- */
- public function loadTemplate() {
- $tpl = $this->template;
- $file = $this->path . DIRECTORY_SEPARATOR . $tpl . '.php';
- if(file_exists($file)) {
- // Write output to buffer
- ob_start();
- extract($this->_); // make vars available via name not array, forbidden vars: $tpl, $file, $output
- // Include template and save output to $output
- include($file);
- $output = ob_get_contents();
- ob_end_clean();
- return $output . PHP_EOL;
- }
- else {
- // Couldn't find template
- // TODO: Throw exception
- // TODO: test if deprecated (see: controller display())
- ErrorHandler::$eh->addError("Could not load template: " . $tpl);
- }
- }
- }
- ?>
|