/MessageDisplay/class/core/View.php
PHP | 91 lines | 30 code | 11 blank | 50 comment | 2 complexity | 83bcfab60514e028a57699d5b753933b MD5 | raw file
- <?php
- /**
- * Prepares the template-variables and loads retungs the output of the template
- * file
- *
- * @author Thomas Schuh based on http://tutorials.lemme.at/mvc-mit-php
- */
- class View
- {
- /**
- * Path to the templates
- * @var string
- * @access private
- */
- private $path = 'tpl';
-
- /**
- * Name of the template, in this case the default template
- * @var string
- * @access private
- */
- private $template = 'page';
- /**
- * Contains the variables, which should be embadded in the template
- * @var array
- * @access private
- */
- private $_ = array();
- /**
- * Assings a key value paare to the template variable
- * @param string $key the key
- * @param string $value the value
- */
- public function assign($key, $value){
- $this->_[$key] = $value;
- }
- /**
- * Sets the name of the template
- *
- * @param string [$template] name of the template
- */
- public function setTemplate($template = 'page'){
- $this->template = $template;
- }
- /**
- * Loads and returns the template-file
- *
- * @return string the output of the template, or an error message
- * if the file does not exits
- */
- public function loadTemplate(){
- /**
- * the name of the template-file (if it was not assinged
- * with the steTemplate() method)
- */
- $tpl = $this->template;
-
- /**
- * Creates the path to the template and checks it the file exists
- */
- $file = $this->path . DIRECTORY_SEPARATOR . $tpl . '.tpl.php';
- print_r($file);
- $exists = file_exists($file);
- if ($exists){
- /**
- * Saves the output first in a buffer
- */
- ob_start();
- /**
- * Includes the template-file and saves it in $output
- */
- include $file;
- $output = ob_get_contents();
- ob_end_clean();
-
- return $output;
- }
- else {
- return 'could not find template';
- }
- }
- }
- ?>