View.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <[email protected]>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.2.0
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Slim;
  34. /**
  35. * View
  36. *
  37. * The view is responsible for rendering a template. The view
  38. * should subclass \Slim\View and implement this interface:
  39. *
  40. * public render(string $template);
  41. *
  42. * This method should render the specified template and return
  43. * the resultant string.
  44. *
  45. * @package Slim
  46. * @author Josh Lockhart
  47. * @since 1.0.0
  48. */
  49. class View
  50. {
  51. /**
  52. * @var string Absolute or relative filesystem path to a specific template
  53. *
  54. * DEPRECATION WARNING!
  55. * This variable will be removed in the near future
  56. */
  57. protected $templatePath = '';
  58. /**
  59. * @var array Associative array of template variables
  60. */
  61. protected $data = array();
  62. /**
  63. * @var string Absolute or relative path to the application's templates directory
  64. */
  65. protected $templatesDirectory;
  66. /**
  67. * Constructor
  68. *
  69. * This is empty but may be implemented in a subclass
  70. */
  71. public function __construct()
  72. {
  73. }
  74. /**
  75. * Get data
  76. * @param string|null $key
  77. * @return mixed If key is null, array of template data;
  78. * If key exists, value of datum with key;
  79. * If key does not exist, null;
  80. */
  81. public function getData($key = null)
  82. {
  83. if (!is_null($key)) {
  84. return isset($this->data[$key]) ? $this->data[$key] : null;
  85. } else {
  86. return $this->data;
  87. }
  88. }
  89. /**
  90. * Set data
  91. *
  92. * If two arguments:
  93. * A single datum with key is assigned value;
  94. *
  95. * $view->setData('color', 'red');
  96. *
  97. * If one argument:
  98. * Replace all data with provided array keys and values;
  99. *
  100. * $view->setData(array('color' => 'red', 'number' => 1));
  101. *
  102. * @param mixed
  103. * @param mixed
  104. * @throws InvalidArgumentException If incorrect method signature
  105. */
  106. public function setData()
  107. {
  108. $args = func_get_args();
  109. if (count($args) === 1 && is_array($args[0])) {
  110. $this->data = $args[0];
  111. } elseif (count($args) === 2) {
  112. $this->data[(string) $args[0]] = $args[1];
  113. } else {
  114. throw new \InvalidArgumentException('Cannot set View data with provided arguments. Usage: `View::setData( $key, $value );` or `View::setData([ key => value, ... ]);`');
  115. }
  116. }
  117. /**
  118. * Append new data to existing template data
  119. * @param array
  120. * @throws InvalidArgumentException If not given an array argument
  121. */
  122. public function appendData($data)
  123. {
  124. if (!is_array($data)) {
  125. throw new \InvalidArgumentException('Cannot append view data. Expected array argument.');
  126. }
  127. $this->data = array_merge($this->data, $data);
  128. }
  129. /**
  130. * Get templates directory
  131. * @return string|null Path to templates directory without trailing slash;
  132. * Returns null if templates directory not set;
  133. */
  134. public function getTemplatesDirectory()
  135. {
  136. return $this->templatesDirectory;
  137. }
  138. /**
  139. * Set templates directory
  140. * @param string $dir
  141. */
  142. public function setTemplatesDirectory($dir)
  143. {
  144. $this->templatesDirectory = rtrim($dir, '/');
  145. }
  146. /**
  147. * Set template
  148. * @param string $template
  149. * @throws RuntimeException If template file does not exist
  150. *
  151. * DEPRECATION WARNING!
  152. * This method will be removed in the near future.
  153. */
  154. public function setTemplate($template)
  155. {
  156. $this->templatePath = $this->getTemplatesDirectory() . '/' . ltrim($template, '/');
  157. if (!file_exists($this->templatePath)) {
  158. throw new \RuntimeException('View cannot render template `' . $this->templatePath . '`. Template does not exist.');
  159. }
  160. }
  161. /**
  162. * Display template
  163. *
  164. * This method echoes the rendered template to the current output buffer
  165. *
  166. * @param string $template Pathname of template file relative to templates directoy
  167. */
  168. public function display($template)
  169. {
  170. echo $this->fetch($template);
  171. }
  172. /**
  173. * Fetch rendered template
  174. *
  175. * This method returns the rendered template
  176. *
  177. * @param string $template Pathname of template file relative to templates directory
  178. * @return string
  179. */
  180. public function fetch($template)
  181. {
  182. return $this->render($template);
  183. }
  184. /**
  185. * Render template
  186. *
  187. * @param string $template Pathname of template file relative to templates directory
  188. * @return string
  189. *
  190. * DEPRECATION WARNING!
  191. * Use `\Slim\View::fetch` to return a rendered template instead of `\Slim\View::render`.
  192. */
  193. public function render($template)
  194. {
  195. $this->setTemplate($template);
  196. extract($this->data);
  197. ob_start();
  198. require $this->templatePath;
  199. return ob_get_clean();
  200. }
  201. }