View クラス

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
パラメータ
パラメータ 規定値 説明
$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)
返り値 a new View object
$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
パラメータ
パラメータ 規定値 説明
$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)
返り値 a new View object
// 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 method sets whether to encode the data or not on a View instance.

Static No
パラメータ
パラメータ 規定値 説明
$filter
true
whether to encode the data or not on a View instance
返り値 Current View object
$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 method allows you to set or change the view filename.

Static No
パラメータ
パラメータ 規定値 説明
$file 必須 the view filename, view files should be relative to the app/views directory and be supplied without the .php extension.
返り値 Current View object
$view = new View();

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

get($key = null, $default = null)

The get method retrieves one or all variables set on a view.

Static No
Parameters
Param Default Description
$key
null
variable name
$default
null
default value to return if the variable isn't set
Returns mixed
Example
// create a view and set some data
$view = new View;
$view->set(array('field1' => 'value1', 'field2' => 'value2');

// get a single value
$field1 = $view->get('field1');

// get a single value, set a default if the field does not exist
$field3 = $view->get('field3', 'value3');

// get all values
$fields = $view->get();

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

The set method assigns a variable to a view.

Static No
パラメータ
パラメータ 規定値 説明
$key 必須 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
返り値 Current View object
$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
パラメータ
パラメータ 規定値 説明
$key 必須 variable name or array of variables
$value
null
the value
返り値 Current View object
$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 method sets a variable accessible for all views.

Static Yes
パラメータ
パラメータ 規定値 説明
$key 必須 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
返り値 Void
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
パラメータ
パラメータ 規定値 説明
$key 必須 variable name or array of variables
$value
null
the value
返り値 Current View object
$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 method 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
パラメータ
パラメータ 規定値 説明
$key 必須 variable name
$value 必須 the value
返り値 Void
View::bind_global('users', $users);

render($file = null)

The set method 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
パラメータ
パラメータ 規定値 説明
$file string
null
the view filename without the extension
返り値 the rendered view file
$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.

パラメータ
パラメータ 規定値 説明
$view string 必須 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
返り値 string, the result from View::render
$html = render('path/to/view', $data_array);