You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage View
  7. * @since 2010-02-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class PHPView implements iView
  12. {
  13. protected $path = '';
  14. protected $variables = array();
  15. protected $extension = '.phtml';
  16. protected $template = '';
  17. public function __construct($config)
  18. {
  19. if (!isset($config['path'])) {
  20. throw new Exception('Configuration must have a "host".');
  21. }
  22. $this->setPath($config['path']);
  23. }
  24. public function getPath()
  25. {
  26. return $this->path;
  27. }
  28. public function setPath($path)
  29. {
  30. $this->path = $path;
  31. }
  32. /**
  33. * @param ViewAction $object
  34. */
  35. public function assignObject($object)
  36. {
  37. foreach (get_object_vars($object) as $name => $value) {
  38. $this->assign($name, $value);
  39. }
  40. }
  41. public function assign($name, $value = null)
  42. {
  43. $this->variables[$name] = $value;
  44. }
  45. public function fetch($template)
  46. {
  47. $this->template = $this->getTemplatePath($template);
  48. unset($template);
  49. extract($this->variables);
  50. ob_start();
  51. if (!is_readable($this->template)) {
  52. ob_clean();
  53. throw new Exception('Template "' . $this->template .'" not found.');
  54. }
  55. include($this->template);
  56. return ob_get_clean();
  57. }
  58. public function escape($var)
  59. {
  60. return htmlentities($var, ENT_QUOTES, 'UTF-8');
  61. }
  62. protected function getTemplatePath($template)
  63. {
  64. return $this->path . $template . $this->extension;
  65. }
  66. }