View.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. use Pimf\Contracts\Renderable;
  10. use Pimf\Util\File;
  11. use Pimf\Contracts\Arrayable;
  12. /**
  13. * A simply view for sending and rendering data.
  14. *
  15. * @package Pimf
  16. * @author Gjero Krsteski <[email protected]>
  17. */
  18. class View implements Renderable
  19. {
  20. /**
  21. * @var string Name of the template.
  22. */
  23. protected $template;
  24. /**
  25. * Contains the variables that are to be embedded in the template.
  26. *
  27. * @var \ArrayObject
  28. */
  29. protected $data;
  30. /**
  31. * Path to templates - is framework restriction.
  32. *
  33. * @var string
  34. */
  35. protected $path;
  36. /**
  37. * @param string $template
  38. * @param array $data
  39. * @param string $path Path to templates if you do not want to use PIMF framework restriction.
  40. */
  41. public function __construct($template = 'default.phtml', array $data = array(), $path = null)
  42. {
  43. $conf = Registry::get('conf');
  44. $this->data = new \ArrayObject($data, \ArrayObject::ARRAY_AS_PROPS);
  45. $this->path = (!$path) ? BASE_PATH . 'app/' . $conf['app']['name'] . '/_templates' : $path;
  46. $this->template = (string)$template;
  47. }
  48. /**
  49. * @param string $template
  50. *
  51. * @return View
  52. */
  53. public function produce($template)
  54. {
  55. $view = clone $this;
  56. $view->template = (string)$template;
  57. return $view;
  58. }
  59. /**
  60. * @param string $template
  61. * @param array|Arrayable $model
  62. *
  63. * @return string
  64. */
  65. public function partial($template, $model = array())
  66. {
  67. $model = ($model instanceof Arrayable) ? $model->toArray() : $model;
  68. return $this->produce($template)->pump($model)->render();
  69. }
  70. /**
  71. * @param string $template
  72. * @param array $model
  73. *
  74. * @return string
  75. */
  76. public function loop($template, array $model = array())
  77. {
  78. $out = '';
  79. foreach ($model as $row) {
  80. $out .= $this->partial($template, $row);
  81. }
  82. return $out;
  83. }
  84. /**
  85. * Assigns a variable to a specific key for the template.
  86. *
  87. * @param string $key The key.
  88. * @param mixed $value The Value.
  89. *
  90. * @return View
  91. */
  92. public function assign($key, $value)
  93. {
  94. $this->data[$key] = $value;
  95. return $this;
  96. }
  97. /**
  98. * Exchange all variables.
  99. *
  100. * @param $model
  101. *
  102. * @return View
  103. */
  104. public function pump(array $model)
  105. {
  106. $this->data->exchangeArray($model);
  107. return $this;
  108. }
  109. /**
  110. * @param string $name
  111. *
  112. * @return mixed
  113. * @throws \OutOfBoundsException If undefined property at the template.
  114. */
  115. public function __get($name)
  116. {
  117. if ($this->data->offsetExists($name)) {
  118. return $this->data->offsetGet($name);
  119. }
  120. $trace = debug_backtrace();
  121. throw new \OutOfBoundsException(
  122. 'undefined property "' . $name . '" at file ' . $trace[0]['file'] . ' line ' . $trace[0]['line']
  123. );
  124. }
  125. /**
  126. * @return string
  127. * @throws \Exception
  128. */
  129. public function render()
  130. {
  131. $level = ob_get_level();
  132. ob_start();
  133. try {
  134. echo $this->reunite();
  135. } catch (\Exception $exception) {
  136. while (ob_get_level() > $level) {
  137. ob_end_clean();
  138. }
  139. throw $exception;
  140. }
  141. return ob_get_clean();
  142. }
  143. /**
  144. * Puts the template an the variables together.
  145. */
  146. public function reunite()
  147. {
  148. include new File(str_replace('/', DS, $this->path . '/' . $this->template));
  149. }
  150. /**
  151. * Act when the view is treated like a string
  152. *
  153. * @return string
  154. */
  155. public function __toString()
  156. {
  157. return $this->render();
  158. }
  159. }