It is recommended page views to be created in init() method:
public function init() {
parent::init();
$v = new Site_Web_Page_View($this); // view name is not specified so default 'default' will be used
$this->add_view($v); // registering the view
}
class Site_Web_Page_View {
__constructor ( Web_Page $page [, string $name [, string $pages_tpl_path [, string $subdir]]] )
}
{include file="`$_path``$_language`/`$_subdir``$_page`/`$_page`_`$_view`_`$_language`.tpl"}).Each WP must return instance of Web_Page_View from its run() or event handling method. You can get and return one of registered (added) view using $this->get_view('view_name');. Example:
public function run() {
... // some functionality
$view = $this->get_view('default'); // getting already created and added (registered) view
return $view; // returning view object
}
You have to specify view name as parameter to get_view(). This name must be name of a view which is already added to the page (in init() method). If you miss to specify view name - 'default' will be used as default value.
Theoretically it is possible page view to be created just before returning it by run() or event handling method but this is not recommendable. Although it is more efficient it lowers the readability of the code. By creating and registering views in init() method (which is on top) you declare loudly: this page can handle these views, so reader can get a quick idea about what to expect.