View Class

The View class acts as an object wrapper for HTML pages with embedded PHP, called "views". Variables can be assigned with the view object and referenced locally within the view.

Read more about using views.

__construct($file = null, $data = null, $filter = null)

Static No
Parameters
Param Default Description
$file
null
the view filename, if not supplied you must supply it later through set_filename or supply it in render. View files should be relative to the app/views directory.
$data
null
array of values
$filter
null
set to true or false to set auto encoding, defaults to main config setting (app/config/config.php)
Returns a new View object
Example
$view = new View('path/to/view', array(
	'menu' => $menu,
	'articles' => $articles,
	'footer_links' => $footer_links,
));

forge($file = null, $data = null, $filter = null)

The forge method returns a new View object.

Static Yes
Parameters
Param Default Description
$file
null
the view filename, if not supplied you must supply it later through set_filename or supply it in render. View files should be relative to the app/views directory and be supplied without the .php file extension.
$data
null
array of values
$filter
null
tet to true or false to set auto encoding, defaults to main config setting (app/config/config.php)
Returns a new View object
Example
// Will create a View object for APPPATH/
$view = View::forge('path/to/view', array(
	'menu' => $menu,
	'articles' => $articles,
	'footer_links' => $footer_links,
));

auto_filter($filter = true)

The auto_filter sets whether to encode the data or not on a View instance.

Static No
Parameters
Param Default Description
$filter
true
whether to encode the data or not on a View instance
Returns Current View object
Example
$view = View::forge('path/to/view', array(
	'menu' => $menu,
	'articles' => $articles,
	'footer_links' => $footer_links,
));

$view->auto_filter();

// or set it to false

$view->auto_filter(false);

// or chain it

$view = View::forge('path/to/view', array(
	'menu' => $menu,
	'articles' => $articles,
	'footer_links' => $footer_links,
))->auto_filter();

set_filename($file)

The set_filename allows you to set or change the view filename.

Static No
Parameters
Param Default Description
$file required the view filename, view files should be relative to the app/views directory and be supplied without the .php extension.
Returns Current View object
Example
$view = new View();

$view->set_filename('path/to/view');

set($key, $value = null, $filter = null)

The set assigns a variable to a view.

Static No
Parameters
Param Default Description
$key required variable name or array of variables
$value
null
the value
$filter
null
set to true or false to encoding, defaults to main auto encoding setting
Returns Current View object
Example
$view = new View;
$view->set('users', $users, false);

// or set an array

$view->set(array(
	'users' => $users,
	'articles' => $articles,
), null, true);

// If you need HTML / Javascript passed you need to set the last param to false
// when output encoding is enabled.
$view->set('example', '<h1>Heading</h1>', false);

set_safe($key, $value = null)

The same as set(), except this defaults to not-encoding the variable on output.

Static No
Parameters
Param Default Description
$key required variable name or array of variables
$value
null
the value
Returns Current View object
Example
$view = new View;
$view->set_safe('users', $users);

// or set an array

$view->set_safe(array(
	'users' => $users,
	'articles' => $articles,
), null);

set_global($key, $value = null, $filter = null)

The set_global sets a variable accessible for all views.

Static Yes
Parameters
Param Default Description
$key required variable name or array of variables
$value
null
the value
$filter
null
set to true or false to encoding, defaults to main auto encoding setting
Returns Void
Example
View::set_global('users', $users, false);

// or set an array

View::set_global(array(
	'users' => $users,
	'articles' => $articles,
), null, true);

bind($key, $value = null)

Assigns a value by reference. The benefit of binding is that values can be altered without re-setting them. It is also possible to bind variables before they have values. Assigned values will be available as a variable within the view file.

Please note: these cannot be cleaned by output encoding and should thus be considered unsafe.

Static No
Parameters
Param Default Description
$key required variable name or array of variables
$value
null
the value
Returns Current View object
Example
$view = new View;
$view->bind('users', $users, false);

// or set an array

$view->bind(array(
	'users' => $users,
	'articles' => $articles,
), null, true);

bind_global($key, $value = null)

The bind_global assigns a global variable by reference accessible for all views.

Please note: these cannot be cleaned by output encoding and should thus be considered unsafe.

Static Yes
Parameters
Param Default Description
$key required variable name
$value required the value
Returns Void
Example
View::bind_global('users', $users);

render($file = null)

The set renders the view object to a string. Global and local data are merged and extracted to create local variables within the view file.

Static No
Parameters
Param Type Default Description
$file string
null
the view filename without the extension
Returns the rendered view file
Example
$html = View::forge()->render('path/to/view');

// or

$html = View::forge('path/to/view')->render();

// or

$html = View::forge()->set_filename('path/to/view')->render();

Procedural helpers

render($view, $data = array(), $auto_filter = null)

The render function is an alias for View::render.

Parameters
Param Type Default Description
$view string required the view filename without the extension
$data array
array()
an array of data to be passed to the view
$auto_filter bool
null
whether to filter the data or not
Returns string, the result from View::render
Example
$html = render('path/to/view', $data_array);