/ 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
Expand |
Embed | Plain Text
<?php /** * LView 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 * * @version 1.0 * @author Victor Villaverde Laan * @link http://www.freelancephp.net/php-view-class/ * @license MIT license */ class LView { /** * @var string View file */ protected $_file = NULL; /** * @var array Containing global vars ( available for all view instances ) */ /** * @var array Containing view paths */ /** * @var array Containing filter callbacks */ /** * @var array Containing global filter callbacks */ /** * @var boolean Always able to use the php short open tag (<?= $var ?>), even * when short_open_tag is disabled in the php configuration */ /** * Constructor * @param string $file Optional, can be set later */ public function __construct( $file = NULL ) { $this->file( $file ); } /** * Renders the view * @return string */ public function __toString() { return $this->render(); } /** * Also get global vars when getting a property value * @param string $key * @return mixed */ public function __get( $key ) { return $this->get( $key ); } /** * Factory method for creating new instance * @param string $file * @return LView */ // return new view instance return new LView( $file ); } /** * Set or Get php short open tag support (<?= $var ?>) * @param boolean $replace Optional * @return boolean|void Returns php short tag support or nothing */ if ( $short_open_tag === NULL ) return self::$_short_open_tag; self::$_short_open_tag = $short_open_tag; } /** * Add view path * @param string $path */ self::$_paths[] = $path; } /** * Set or Get the view file * @param string $view_file optional * @return string|this File or this */ if ( $view_file === NULL ) return $this->_file; $this->_file = $view_file; return $this; } /** * Get a var value ( if not exists it will look in the global vars ) * @param string $key * @param mixed $default_value * @return mixed */ public function get( $key, $default_value = NULL ) { return $this->{ $key }; return self::$_global_vars[ $key ]; return $default_value; } /** * Get or Set a var * @param string|array $key Can also give array of values, f.e. array( 'var1' => 'value1', 'var2' => 'value2' ) * @param mixed $value * @return this */ public function set( $key, $value = NULL ) { // array of values foreach ( $key AS $k => $value ) $this->{ $k } = $value; } else { $this->{ $key } = $value; } return $this; } /** * Set a global var ( available for all views ) * @param string|array $key Can also give array of values, f.e. array( 'var1' => 'value1', 'var2' => 'value2' ) * @param mixed $value * @return string */ // array of values foreach ( $key AS $k => $value ) self::$_global_vars[ $k ] = $value; } else { self::$_global_vars[ $key ] = $value; } } /** * Check if view file exists * @param string $file Check if given view file exists * @return boolean */ // check if file exists return TRUE; // check if file can be found in the paths foreach ( self::$_paths AS $path ) { return TRUE; } return FALSE; } /** * Add/remove filter for after fetching * @param string $key * @param mixed $callback * @return this */ public function add_filter( $key, $callback = NULL ) { if ( $callback === NULL ) { $callback = $key; $key = NULL; } else { // remove filter if exists } } // add filter as a callback function if ( $key === NULL ) { $this->_filters[] = $callback; } else { $this->_filters[ $key ] = $callback; } return $this; } /** * Add/remove global filter ( used with all views ) * @param string $key * @param mixed $callback * @return this */ if ( $callback === NULL ) { $callback = $key; $key = NULL; } else { // remove global filter if exists } } // add global filter as a callback function if ( $key === NULL ) { self::$_global_filters[] = $callback; } else { self::$_global_filters[ $key ] = $callback; } } /** * Render the view content * @return string * @throw Exception */ public function render() { // check if view file exists $view_file = NULL; $view_file = $this->_file; } else { foreach ( self::$_paths AS $path ) { $view_file = $path . $this->_file; } if ( $view_file === NULL ) throw new Exception( get_class( $this ) . ': The file "' . $this->_file . '" could not be fetched.' ); } // get all vars // add global vars // extract vars to global namespace // start output buffer // replace short php tags to normal (in case the server doesn't support it) if ( self::$_short_open_tag ) { } else { include( $view_file ); } // get the view content $content = ob_get_contents(); // set filters $filters = array_merge( self::$_global_filters, $this->_filters ); // call filters foreach ( $filters as $key => $callback ) { if ( is_callable( $callback ) ) $content = call_user_func( $callback, $content, $this ); } // clean output buffer ob_end_clean(); return $content; } /** * Echo view * @return this */ public function show() { echo $this->render(); return $this; } } /*?> // ommit closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */
You need to login to post a comment.
