Posted By

freelancephp on 02/14/11


Tagged

class view MVC


Versions (?)

LView - PHP View Class


 / Published in: PHP
 

URL: http://www.freelancephp.net/php-view-class/

View class that contains features like: - get vars inside the view with: 1) $var1 2) $this->var1 3) or $this->get( 'var1', 'defaultValue' ) - factory method for direct chaining: LView::factory( 'test.php' )->set( 'var1', 'value1' )->show(); - set filter callbacks that will be executed right after rendering - set global vars and global filters to be available for all views

  1. <?php
  2. /**
  3.  * LView Class
  4.  *
  5.  * View class that contains features like:
  6.  * - get vars inside the view with:
  7.  * 1) $var1
  8.  * 2) $this->var1
  9.  * 3) or $this->get( 'var1', 'defaultValue' )
  10.  * - factory method for direct chaining:
  11.  * LView::factory( 'test.php' )->set( 'var1', 'value1' )->show();
  12.  * - set filter callbacks that will be executed right after rendering
  13.  * - set global vars and global filters to be available for all views
  14.  *
  15.  * @version 1.0
  16.  * @author Victor Villaverde Laan
  17.  * @link http://www.freelancephp.net/php-view-class/
  18.  * @license MIT license
  19.  */
  20. class LView
  21. {
  22.  
  23. /**
  24. * @var string View file
  25. */
  26. protected $_file = NULL;
  27.  
  28. /**
  29. * @var array Containing global vars ( available for all view instances )
  30. */
  31. protected static $_global_vars = array();
  32.  
  33. /**
  34. * @var array Containing view paths
  35. */
  36. protected static $_paths = array();
  37.  
  38. /**
  39. * @var array Containing filter callbacks
  40. */
  41. protected $_filters = array();
  42.  
  43. /**
  44. * @var array Containing global filter callbacks
  45. */
  46. protected static $_global_filters = array();
  47.  
  48. /**
  49. * @var boolean Always able to use the php short open tag (<?= $var ?>), even
  50. * when short_open_tag is disabled in the php configuration
  51. */
  52. protected static $_short_open_tag = FALSE;
  53.  
  54.  
  55. /**
  56. * Constructor
  57. * @param string $file Optional, can be set later
  58. */
  59. public function __construct( $file = NULL ) {
  60. $this->file( $file );
  61. }
  62.  
  63. /**
  64. * Renders the view
  65. * @return string
  66. */
  67. public function __toString() {
  68. return $this->render();
  69. }
  70.  
  71. /**
  72. * Also get global vars when getting a property value
  73. * @param string $key
  74. * @return mixed
  75. */
  76. public function __get( $key ) {
  77. return $this->get( $key );
  78. }
  79.  
  80. /**
  81. * Factory method for creating new instance
  82. * @param string $file
  83. * @return LView
  84. */
  85. public static function factory( $file = NULL ) {
  86. // return new view instance
  87. return new LView( $file );
  88. }
  89.  
  90. /**
  91. * Set or Get php short open tag support (<?= $var ?>)
  92. * @param boolean $replace Optional
  93. * @return boolean|void Returns php short tag support or nothing
  94. */
  95. public static function short_open_tag( $short_open_tag = NULL ) {
  96. if ( $short_open_tag === NULL )
  97. return self::$_short_open_tag;
  98.  
  99. self::$_short_open_tag = $short_open_tag;
  100. }
  101.  
  102. /**
  103. * Add view path
  104. * @param string $path
  105. */
  106. public static function add_path( $path ) {
  107. self::$_paths[] = $path;
  108. }
  109.  
  110. /**
  111. * Set or Get the view file
  112. * @param string $view_file optional
  113. * @return string|this File or this
  114. */
  115. public function file( $view_file = NULL ) {
  116. if ( $view_file === NULL )
  117. return $this->_file;
  118.  
  119. $this->_file = $view_file;
  120. return $this;
  121. }
  122.  
  123. /**
  124. * Get a var value ( if not exists it will look in the global vars )
  125. * @param string $key
  126. * @param mixed $default_value
  127. * @return mixed
  128. */
  129. public function get( $key, $default_value = NULL ) {
  130. if ( isset( $this->{ $key } ) )
  131. return $this->{ $key };
  132.  
  133. if ( isset( self::$_global_vars[ $key ] ) )
  134. return self::$_global_vars[ $key ];
  135.  
  136. return $default_value;
  137. }
  138.  
  139. /**
  140. * Get or Set a var
  141. * @param string|array $key Can also give array of values, f.e. array( 'var1' => 'value1', 'var2' => 'value2' )
  142. * @param mixed $value
  143. * @return this
  144. */
  145. public function set( $key, $value = NULL ) {
  146. if ( is_array( $key ) ) {
  147. // array of values
  148. foreach ( $key AS $k => $value )
  149. $this->{ $k } = $value;
  150. } else {
  151. $this->{ $key } = $value;
  152. }
  153.  
  154. return $this;
  155. }
  156.  
  157. /**
  158. * Set a global var ( available for all views )
  159. * @param string|array $key Can also give array of values, f.e. array( 'var1' => 'value1', 'var2' => 'value2' )
  160. * @param mixed $value
  161. * @return string
  162. */
  163. public static function set_global( $key, $value = NULL ) {
  164. if ( is_array( $key ) ) {
  165. // array of values
  166. foreach ( $key AS $k => $value )
  167. self::$_global_vars[ $k ] = $value;
  168. } else {
  169. self::$_global_vars[ $key ] = $value;
  170. }
  171. }
  172.  
  173. /**
  174. * Check if view file exists
  175. * @param string $file Check if given view file exists
  176. * @return boolean
  177. */
  178. public static function exists( $file ) {
  179. // check if file exists
  180. if ( file_exists( $file ) )
  181. return TRUE;
  182.  
  183. // check if file can be found in the paths
  184. foreach ( self::$_paths AS $path ) {
  185. if ( file_exists( $path . $file ) )
  186. return TRUE;
  187. }
  188.  
  189. return FALSE;
  190. }
  191.  
  192. /**
  193. * Add/remove filter for after fetching
  194. * @param string $key
  195. * @param mixed $callback
  196. * @return this
  197. */
  198. public function add_filter( $key, $callback = NULL ) {
  199. if ( $callback === NULL ) {
  200. if ( is_callable( $key ) ) {
  201. $callback = $key;
  202. $key = NULL;
  203. } else {
  204. // remove filter if exists
  205. if ( isset( $this->_filters[ $key ] ) )
  206. unset( $this->_filters[ $key ] );
  207. }
  208. }
  209.  
  210. // add filter as a callback function
  211. if ( $key === NULL ) {
  212. $this->_filters[] = $callback;
  213. } else {
  214. $this->_filters[ $key ] = $callback;
  215. }
  216.  
  217. return $this;
  218. }
  219.  
  220. /**
  221. * Add/remove global filter ( used with all views )
  222. * @param string $key
  223. * @param mixed $callback
  224. * @return this
  225. */
  226. public static function add_global_filter( $key, $callback = NULL ) {
  227. if ( $callback === NULL ) {
  228. if ( is_callable( $key ) ) {
  229. $callback = $key;
  230. $key = NULL;
  231. } else {
  232. // remove global filter if exists
  233. if ( isset( self::$_global_filters[ $key ] ) )
  234. unset( self::$_global_filters[ $key ] );
  235. }
  236. }
  237.  
  238. // add global filter as a callback function
  239. if ( $key === NULL ) {
  240. self::$_global_filters[] = $callback;
  241. } else {
  242. self::$_global_filters[ $key ] = $callback;
  243. }
  244. }
  245.  
  246. /**
  247. * Render the view content
  248. * @return string
  249. * @throw Exception
  250. */
  251. public function render() {
  252. // check if view file exists
  253. $view_file = NULL;
  254.  
  255. if ( file_exists( $this->_file ) ) {
  256. $view_file = $this->_file;
  257. } else {
  258. foreach ( self::$_paths AS $path ) {
  259. if ( file_exists( $path . $this->_file ) )
  260. $view_file = $path . $this->_file;
  261. }
  262.  
  263. if ( $view_file === NULL )
  264. throw new Exception( get_class( $this ) . ': The file "' . $this->_file . '" could not be fetched.' );
  265. }
  266.  
  267. // get all vars
  268. $vars = get_object_vars( $this );
  269.  
  270. // add global vars
  271. $vars = array_merge( self::$_global_vars, $vars );
  272.  
  273. // extract vars to global namespace
  274. extract( $vars, EXTR_SKIP );
  275.  
  276. // start output buffer
  277.  
  278. // replace short php tags to normal (in case the server doesn't support it)
  279. if ( self::$_short_open_tag ) {
  280. echo eval( '?>' . str_replace( '<?=', '<?php echo ', file_get_contents( $view_file ) ) );
  281. } else {
  282. include( $view_file );
  283. }
  284.  
  285. // get the view content
  286. $content = ob_get_contents();
  287.  
  288. // set filters
  289. $filters = array_merge( self::$_global_filters, $this->_filters );
  290.  
  291. // call filters
  292. foreach ( $filters as $key => $callback ) {
  293. if ( is_callable( $callback ) )
  294. $content = call_user_func( $callback, $content, $this );
  295. }
  296.  
  297. // clean output buffer
  298. ob_end_clean();
  299.  
  300. return $content;
  301. }
  302.  
  303. /**
  304. * Echo view
  305. * @return this
  306. */
  307. public function show() {
  308. echo $this->render();
  309. return $this;
  310. }
  311.  
  312. }
  313.  
  314. /*?> // ommit closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */

Report this snippet  

You need to login to post a comment.