Fuel Documentation

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, $auto_encode = 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
$auto_encode 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,
), false);

factory($file = null, $data = null, $auto_encode = null)

The factory 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
$auto_encode 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::factory('path/to/view', array(
	'menu' => $menu,
	'articles' => $articles,
	'footer_links' => $footer_links,
), false);

auto_encode($encode = true)

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

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

$view->auto_encode();

// or set it to false

$view->auto_encode(false);

// or chain it

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

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, $encode = 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
$encode 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_global($key, $value = null, $encode = 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
$encode 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 or array of variables
$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::factory()->render('path/to/view');

// or

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

// or

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

Procedural helpers

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

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
Returns string, the result from View::render
Example
$html = render('path/to/view', $data_array);