LView – 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

<?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

{</code>

<code>/**

* @var string  View file

*/

protected $_file = NULL;

/**

* @var array  Containing global vars ( available for all view instances )

*/

protected static $_global_vars = array();

/**

* @var array  Containing view paths

*/

protected static $_paths = array();

/**

* @var array  Containing filter callbacks

*/

protected $_filters = array();

/**

* @var array  Containing global filter callbacks

*/

protected static $_global_filters = array();

/**

* @var boolean Always able to use the php short open tag (<!--?= $var ?-->), even

*				when short_open_tag is disabled in the php configuration

*/

protected static $_short_open_tag = FALSE;

/**

* 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

*/

public static function factory( $file = NULL ) {

// 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

*/

public static function short_open_tag( $short_open_tag = NULL ) {

if ( $short_open_tag === NULL )

return self::$_short_open_tag;

self::$_short_open_tag = $short_open_tag;

}

/**

* Add view path

* @param string $path

*/

public static function add_path( $path ) {

self::$_paths[] = $path;

}

/**

* Set or Get the view file

* @param string $view_file optional

* @return string|this File or this

*/

public function file( $view_file = NULL ) {

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 ) {

if ( isset( $this->{ $key } ) )

return $this->{ $key };

if ( isset( self::$_global_vars[ $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 ) {

if ( is_array( $key ) ) {

// 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

*/

public static function set_global( $key, $value = NULL ) {

if ( is_array( $key ) ) {

// 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

*/

public static function exists( $file ) {

// check if file exists

if ( file_exists( $file ) )

return TRUE;

// check if file can be found in the paths

foreach ( self::$_paths AS $path ) {

if ( file_exists( $path . $file ) )

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 ) {

if ( is_callable( $key ) ) {

$callback = $key;

$key = NULL;

} else {

// remove filter if exists

if ( isset( $this->_filters[ $key ] ) )

unset( $this->_filters[ $key ] );

}

}

// 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

*/

public static function add_global_filter( $key, $callback = NULL ) {

if ( $callback === NULL ) {

if ( is_callable( $key ) ) {

$callback = $key;

$key = NULL;

} else {

// remove global filter if exists

if ( isset( self::$_global_filters[ $key ] ) )

unset( self::$_global_filters[ $key ] );

}

}

// 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;

if ( file_exists( $this->_file ) ) {

$view_file = $this->_file;

} else {

foreach ( self::$_paths AS $path ) {

if ( file_exists( $path . $this->_file ) )

$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

$vars = get_object_vars( $this );

// add global vars

$vars = array_merge( self::$_global_vars, $vars );

// extract vars to global namespace

extract( $vars, EXTR_SKIP );

// start output buffer

ob_start();

// replace short php tags to normal (in case the server doesn't support it)

if ( self::$_short_open_tag ) {

echo eval( '?>' . str_replace( '<!--?=', '<?php echo ', file_get_contents( $view_file ) ) );<br /-->
} 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;

}

}

?>

Go to Source

This entry was posted in PHP and tagged , , , , , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?