PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/MessageDisplay/class/core/View.php

https://github.com/Jokl/spielwiese
PHP | 91 lines | 30 code | 11 blank | 50 comment | 2 complexity | 83bcfab60514e028a57699d5b753933b MD5 | raw file
  1. <?php
  2. /**
  3. * Prepares the template-variables and loads retungs the output of the template
  4. * file
  5. *
  6. * @author Thomas Schuh based on http://tutorials.lemme.at/mvc-mit-php
  7. */
  8. class View
  9. {
  10. /**
  11. * Path to the templates
  12. * @var string
  13. * @access private
  14. */
  15. private $path = 'tpl';
  16. /**
  17. * Name of the template, in this case the default template
  18. * @var string
  19. * @access private
  20. */
  21. private $template = 'page';
  22. /**
  23. * Contains the variables, which should be embadded in the template
  24. * @var array
  25. * @access private
  26. */
  27. private $_ = array();
  28. /**
  29. * Assings a key value paare to the template variable
  30. * @param string $key the key
  31. * @param string $value the value
  32. */
  33. public function assign($key, $value){
  34. $this->_[$key] = $value;
  35. }
  36. /**
  37. * Sets the name of the template
  38. *
  39. * @param string [$template] name of the template
  40. */
  41. public function setTemplate($template = 'page'){
  42. $this->template = $template;
  43. }
  44. /**
  45. * Loads and returns the template-file
  46. *
  47. * @return string the output of the template, or an error message
  48. * if the file does not exits
  49. */
  50. public function loadTemplate(){
  51. /**
  52. * the name of the template-file (if it was not assinged
  53. * with the steTemplate() method)
  54. */
  55. $tpl = $this->template;
  56. /**
  57. * Creates the path to the template and checks it the file exists
  58. */
  59. $file = $this->path . DIRECTORY_SEPARATOR . $tpl . '.tpl.php';
  60. print_r($file);
  61. $exists = file_exists($file);
  62. if ($exists){
  63. /**
  64. * Saves the output first in a buffer
  65. */
  66. ob_start();
  67. /**
  68. * Includes the template-file and saves it in $output
  69. */
  70. include $file;
  71. $output = ob_get_contents();
  72. ob_end_clean();
  73. return $output;
  74. }
  75. else {
  76. return 'could not find template';
  77. }
  78. }
  79. }
  80. ?>