Presenter Class

The Presenter class acts as an object wrapper for "views", and is used to abstract all logic related to the view away from the controller. Read more about using presenters. Like a Controller, a Presenter supports before() and after() methods, which you can use for code generic to all methods for view prepping.

forge($presenter, $method = 'view', $auto_filter = null, $view = null)

The forge method returns a new Presenter object.

Static Yes
Parameters
Param Default Description
$presenter required Name of the presenter, and by default of its associated view, using View notation.
$method
'view'
Name of the presenter method that will prep the View for rendering. You can have multiple prep methods defined in the Presenter, for example to generate different layouts of the same view.
$auto_filter
null
set to true or false to set auto encoding, defaults to main config setting (app/config/config.php)
$view
null
Custom view name, used if the view to be loaded can not be determined from the presenter name
Returns a new Presenter object
Example
// Will create a Presenter object
// for the APPPATH/views/admin/index.php view file
// using the Presenter_Admin_Index class in APPPATH/classes/presenter/admin/index.php

$presenter = Presenter::Forge('admin/index');

// use the custom() method in the presenter to render the view differently
$presenter = Presenter::Forge('admin/index', 'custom');

// use a custom view
$presenter = Presenter::Forge('admin/index', 'custom', null, 'admin/different/view');

// or even a custom view object
$view = View::forge('admin/different/view', array(
	'menu' => $menu,
	'articles' => $articles,
	'footer_links' => $footer_links,
));

$presenter = Presenter::Forge('admin/index', 'custom', null, $view);

get_view()

The get_view method returns the View instance associated with the Presenter object.

Static No
Parameters None
Returns The associated View object
Example
// create a presenter instance
$presenter = Presenter::Forge('admin/index');

// and the view associated with it
$view = $presenter->get_view();

view()

The view method is the default method which is called when the Presenter is rendered. It contains the logic to prep the view for rendering.

Static No
Parameters None
Example See the Presenter overview page.

A presenter can contain multiple prepping methods, which are used when you need multiple sets of logic for generating the view. You could for example have a custom method that generates the view without headers and footers, or one that creates a custom view optimized for mobile devices. It allows you to keep the controller generic, it doesn't need to know what output has to be generated by the presenter.

set_view($view = null)

The set_view method can be used to change the View associated with the presenter.

Static No
Parameters
Param Default Description
$view
null
Name of the view file, or a valid View object.
Example
// create a presenter instance
$presenter = Presenter::Forge('admin/public');

// some code here that changes the associated view
if ($something)
{
	$presenter->set_view('admin/member');
}

When you use this method, any variables set on or bound to the original View object will be transferred to the new View.

View object compatibility

The Presenter class is interchangeable with the View class in your code. This means that if you start with Views, and later realize you need to have additional view prepping logic and you want to use a Presenter instead, you don't have to change your controller code, other than forging a Presenter instead of a View.

To support this, the Presenter exposes the set(), set_safe(), bind(), auto_filter() and render() methods of the associated View object. Is also has magic getter and setters to access and set properties on the associated View object.

The Presenter doesn't support the static methods set_global() and bind_global(), if you need global variables for your views, you still have to set them on the View class. For the Presenter, this is transparent.

If you want to extend the Presenter to be able to swap View instances after the Presenter object has been created, know that the presenter doesn't have its own data container. Instead, it uses the associated View object to store all data, which means that if you swap that View object by a new one, you lose all variables set on it!