PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/slim/Slim/View.php

http://github.com/eryx/php-framework-benchmark
PHP | 167 lines | 48 code | 12 blank | 107 comment | 9 complexity | ee50ff91e81b218dd47e52dc18800851 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <info@joshlockhart.com>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 1.5.0
  10. *
  11. * MIT LICENSE
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this software and associated documentation files (the
  15. * "Software"), to deal in the Software without restriction, including
  16. * without limitation the rights to use, copy, modify, merge, publish,
  17. * distribute, sublicense, and/or sell copies of the Software, and to
  18. * permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be
  22. * included in all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. /**
  33. * Slim View
  34. *
  35. * The View is responsible for rendering and/or displaying a template.
  36. * It is recommended that you subclass View and re-implement the
  37. * `View::render` method to use a custom templating engine such as
  38. * Smarty, Twig, Mustache, etc. It is important that `View::render`
  39. * `return` the final template output. Do not `echo` the output.
  40. *
  41. * @package Slim
  42. * @author Josh Lockhart <info@joshlockhart.com>
  43. * @since Version 1.0
  44. */
  45. class Slim_View {
  46. /**
  47. * @var array Key-value array of data available to the template
  48. */
  49. protected $data = array();
  50. /**
  51. * @var string Absolute or relative path to the templates directory
  52. */
  53. protected $templatesDirectory;
  54. /**
  55. * Constructor
  56. *
  57. * This is empty but may be overridden in a subclass
  58. */
  59. public function __construct() {}
  60. /**
  61. * Get data
  62. * @param string $key
  63. * @return array|mixed|null All View data if no $key, value of datum
  64. * if $key, or NULL if $key but datum
  65. * does not exist.
  66. */
  67. public function getData( $key = null ) {
  68. if ( !is_null($key) ) {
  69. return isset($this->data[$key]) ? $this->data[$key] : null;
  70. } else {
  71. return $this->data;
  72. }
  73. }
  74. /**
  75. * Set data
  76. *
  77. * This method is overloaded to accept two different method signatures.
  78. * You may use this to set a specific key with a specfic value,
  79. * or you may use this to set all data to a specific array.
  80. *
  81. * USAGE:
  82. *
  83. * View::setData('color', 'red');
  84. * View::setData(array('color' => 'red', 'number' => 1));
  85. *
  86. * @param string|array
  87. * @param mixed Optional. Only use if first argument is a string.
  88. * @return void
  89. * @throws InvalidArgumentException If incorrect method signature
  90. */
  91. public function setData() {
  92. $args = func_get_args();
  93. if ( count($args) === 1 && is_array($args[0]) ) {
  94. $this->data = $args[0];
  95. } else if ( count($args) === 2 ) {
  96. $this->data[(string)$args[0]] = $args[1];
  97. } else {
  98. throw new InvalidArgumentException('Cannot set View data with provided arguments. Usage: `View::setData( $key, $value );` or `View::setData([ key => value, ... ]);`');
  99. }
  100. }
  101. /**
  102. * Append data to existing View data
  103. * @param array $data
  104. * @return void
  105. */
  106. public function appendData( array $data ) {
  107. $this->data = array_merge($this->data, $data);
  108. }
  109. /**
  110. * Get templates directory
  111. * @return string|null Path to templates directory without trailing slash
  112. */
  113. public function getTemplatesDirectory() {
  114. return $this->templatesDirectory;
  115. }
  116. /**
  117. * Set templates directory
  118. * @param string $dir
  119. * @return void
  120. * @throws RuntimeException If directory is not a directory or does not exist
  121. */
  122. public function setTemplatesDirectory( $dir ) {
  123. if ( !is_dir($dir) ) {
  124. throw new RuntimeException('Cannot set View templates directory to: ' . $dir . '. Directory does not exist.');
  125. }
  126. $this->templatesDirectory = rtrim($dir, '/');
  127. }
  128. /**
  129. * Display template
  130. *
  131. * This method echoes the rendered template to the current output buffer
  132. *
  133. * @param string $template Path to template file relative to templates directoy
  134. * @return void
  135. */
  136. public function display( $template ) {
  137. echo $this->render($template);
  138. }
  139. /**
  140. * Render template
  141. * @param string $template Path to template file relative to templates directory
  142. * @return string Rendered template
  143. * @throws RuntimeException If template does not exist
  144. */
  145. public function render( $template ) {
  146. extract($this->data);
  147. $templatePath = $this->getTemplatesDirectory() . '/' . ltrim($template, '/');
  148. if ( !file_exists($templatePath) ) {
  149. throw new RuntimeException('View cannot render template `' . $templatePath . '`. Template does not exist.');
  150. }
  151. ob_start();
  152. require $templatePath;
  153. return ob_get_clean();
  154. }
  155. }